Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xml serialization - remove namespace

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; } } 
like image 301
Pankaj Avatar asked Nov 09 '11 06:11

Pankaj


People also ask

Should I use the XML serializer?

The only not-so-perfect part of using the XML Serializer is that it by default adds namespace and schema attributes that point at the W3C standard declarations – but it’s just as easy to remove these so your resulting XML looks perfectly like your original XML file.

How to remove namespaces from an XML document?

RemoveNamespacePrefix(doc.Root); Federation of State Medi... Namespaces in XML documents are controlled via the XmlNamespaceManager and NameTable classes. In theory you just remove the namespaces from the document. But you'll also have to remove the prefixes from any impacted objects.

How to remove XSD and XSI namespaces from an XML file?

If I give prefix as empty , xsd and xsi both namespaces are gone. Just do a string.Replace on the raw xml and replace the text you want gone with "".

How to remove namespace aliases in XmlSerializer?

If you just want to remove the namespace aliases, then as already shown you can use XmlSerializerNamespaces to force XmlSerializer to use the namespace explicitly (i.e. xmlns="blah") on each element, rather than declaring an alias and using the alias instead.


1 Answers

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); 
like image 165
Vladimir Perevalov Avatar answered Oct 06 '22 20:10

Vladimir Perevalov