29

I am serializing an object using c#. I am getting result in this format given bellow

<?xml version="1.0"?>
<Users xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Users>
        <User Id="11005477969327" CreateDate="06/03/2011" LastSendDate="1/1/0001" />
        <User Id="11034688201594" CreateDate="04/22/2012" LastSendDate="1/1/0001" />
    <Users 
</User>

    

But I want result in this format.

<?xml version="1.0"?>
<Users>
  <User Id="11005477969327" SendDate="1/1/0001" NextSendDate="2/7/2012 11:13:30 AM" />
  <User Id="11034688201594" SendDate="1/1/0001" NextSendDate="2/7/2012 11:13:30 AM" />
</Users>

Here's my code:

public class Users
{
    [XmlArray("Users")]
    public List<User> ListData { get; set; }

    public string GetXML()
    {
        System.IO.MemoryStream ms = new System.IO.MemoryStream();

        XmlSerializer sr = new XmlSerializer(typeof(Users));
        sr.Serialize(ms, this);

        ms.Position = 0;
        return System.Text.Encoding.UTF8.GetString(ms.ToArray());
    }
}

public class User
{
    [XmlAttribute("Id")]
    public Int64 UserId { get; set; }

    [XmlAttribute("CreateDate")]
    public string CreateDate { get; set; }

    [XmlAttribute("LastSendDate")]
    public string LastSendDate { get; set; }
}
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
Pankaj
  • 4,419
  • 16
  • 50
  • 72
  • 1
    maybe these will help http://stackoverflow.com/questions/625927/omitting-all-xsi-and-xsd-namespaces-when-serializing-an-object-in-net http://stackoverflow.com/questions/6037948/remove-namespaces-during-xml-serialization – Mithir Nov 09 '11 at 06:26
  • Possible duplicate of [Omitting all xsi and xsd namespaces when serializing an object in .NET?](http://stackoverflow.com/questions/625927/omitting-all-xsi-and-xsd-namespaces-when-serializing-an-object-in-net) – Human_AfterAll Mar 13 '17 at 18:52

3 Answers3

77

You should simply replace [XmlArray("Users")] with [XmlElement("User")]

This attribute tell serializer, that you want to store all those User items under particular node "Users", if you replace it with XmlElement, serialzer will store all those users inline (right under first Users tag) just as you like it.

As for xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" namespaces. They are added automatically, but they are harmless, since all your elements are in the default namespace. You may add following code to your XmlSerializer call in order to remove those:

var xns = new XmlSerializerNamespaces();
var serializer = new XmlSerializer(users.GetType());
xns.Add(string.Empty, string.Empty);
//...
serializer.Serialize(stream, users, xns);
Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
Vladimir Perevalov
  • 4,059
  • 18
  • 22
  • 1
    You might also find the XmlSettings useful for the layout of your XML file: `XmlWriter writer; XmlSettings.Indent = true; XmlSettings.NewLineOnAttributes = true; writer = XmlWriter.Create(Path.Combine(strOutputDir, strXmlFileName), XmlSettings);` serializer.Serialize(writer, VcdMsg); writer.Close(); – MoonKnight Nov 09 '11 at 08:10
  • 1
    +1: Just what I needed for the same problem (and a good explanation). @Pankaj: Why is this not marked as the correct answer? – iCollect.it Ltd Jun 14 '12 at 14:58
  • @Vladimir Perevalov: >>but they are harmless<< No, they aren't. If resolving the dns name causes an exception, for example because there's no such dns entry, then XmlDocument.Load will throw an exception when loading (e.g. the svg namespace)... – Stefan Steiger Sep 18 '14 at 08:44
  • @Quandary in the current question we speak about XML generated by specific tool (XmlSerializer), which should generate them correctly. What I meant: in the example above it does not matter if those namespaces are declared or not. – Vladimir Perevalov Sep 22 '14 at 13:19
  • I think there is no need for this line `xns.Add(string.Empty, string.Empty);` – Christian Gollhardt Aug 03 '15 at 08:30
10

You should write:

XmlSerializer sr = new XmlSerializer(typeof(Users));
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("","");
sr.Serialize(ms, this, ns);
-10

Quickie solution - convert to string and then remove thus - Uses a button click to start execution.

Imports System.IO
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim mydoc As XElement = XElement.Load("C:\Users\Documents\myfile.XML")
    Dim y As String = mydoc.ToString
    Dim z As String = mydoc.Name.Namespace.ToString
    Dim getrid As String = " xmlns=" & Chr(34) & z & Chr(34)
    y = Replace(y, getrid, "")
    Dim tr As TextReader = New StringReader(y)
    Dim newdoc As XElement = XElement.Load(tr)
    tr.Close()
    Debug.Print(newdoc.ToString)
End Sub
End Class
Sushil
  • 2,837
  • 4
  • 21
  • 29