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>