2

How to remove xmlns from all the inner nodes of the xml. I was able to remove the xmlns from the root node but still the inner node has all the xmlns in the inner node.

public class Program
{
    public static void Main()
    {

        List<Person> students = new List<Person>();

        Student std1 = new Student() { Name="Name1", StudentId = 1};
        students.Add(std1);

        Student std2 = new Student() { Name = "Name2", StudentId = 2 };
        students.Add(std2);

        string data = students.ToList().ToXML();
    }
}

[System.Xml.Serialization.XmlInclude(typeof(Student))]
public class Person
{
    public string Name { get; set; }
}

public class Student : Person
{
    public int StudentId { get; set; }
}

ToXML()

public static string ToXML<T>(this T value)
    {
        if (value.IsNull()) return string.Empty;

        var xmlSerializer = new XmlSerializer(typeof(T));
        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        ns.Add("", "");

        using (var stringWriter = new StringWriter())
        {
            using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings { OmitXmlDeclaration = true, Indent = false }))
            {
                xmlSerializer.Serialize(xmlWriter, value, ns);
                return stringWriter.ToString();
            }
        }
    }

Output is

<ArrayOfPerson>
    <Person p2:type="Student" xmlns:p2="http://www.w3.org/2001/XMLSchema-instance">
        <Name>Name1</Name>
        <StudentId>1</StudentId>
    </Person>
    <Person p2:type="Student" xmlns:p2="http://www.w3.org/2001/XMLSchema-instance">
        <Name>Name2</Name>
        <StudentId>2</StudentId>
    </Person>
</ArrayOfPerson>

Expected output is

<ArrayOfPerson>
    <Person>
        <Name>Name1</Name>
        <StudentId>1</StudentId>
    </Person>
    <Person>
        <Name>Name2</Name>
        <StudentId>2</StudentId>
    </Person>
</ArrayOfPerson>
Gopi
  • 5,656
  • 22
  • 80
  • 146

4 Answers4

0

After you do the XML serialization in your code you can apply the RemoveAllNamespaces() function from this answer: How to remove all namespaces from XML with C#?

  • Thanks, I did went through it but I am looking for a simple and elegant solution that can be add in ToXML(), also there will be thousands of objects in my list in real time, so I am concerned about the performance too – Gopi Jun 24 '17 at 10:55
0

This is being added because you're serialising a list of Person but are using a sub-type of Student. The type attributes are required for the serialiser to determine the actual type being used.

If you don't want these attributes and you only have a single type of 'person', then you should collapse the definitions of Student and Person:

public class Person
{
    public string Name { get; set; }
    public int StudentId { get; set; }
}
Charles Mager
  • 25,735
  • 2
  • 35
  • 45
0
[XmlElement("Person", Type = typeof(Student))]
List<Person> students = new List<Person>();

then remove your include Took me forever to figure this one out

0

In my case the output was like this:

<ArrayOfPerson>
    <Person>
        <Name xmlns:p2="http://www.w3.org/2001/XMLSchema-instance"/>
        <StudentId>1</StudentId>
    </Person>
</ArrayOfPerson>

The xmlns polluted the child property, this is happening when the property is empty (null) and the property name in the class is fixtured with
IsNullable = true

Like:

[XmlElement(IsNullable = true)]
public string Name { get; set; }

So just change it false in case you want to remove the xmlns:p2 from the child property:

[XmlElement(IsNullable = false)]
public string Name { get; set; }
Shahar Shokrani
  • 7,598
  • 9
  • 48
  • 91