[英]Remove p2:type="<<type>>" xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" from xml
How to remove xmlns from all the inner nodes of the xml.如何从 xml 的所有内部节点中删除 xmlns。 I was able to remove the xmlns from the root node but still the inner node has all the xmlns in the inner node.我能够从根节点中删除 xmlns,但内部节点仍然具有内部节点中的所有 xmlns。
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() 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 Output 是
<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预期 output 是
<ArrayOfPerson>
<Person>
<Name>Name1</Name>
<StudentId>1</StudentId>
</Person>
<Person>
<Name>Name2</Name>
<StudentId>2</StudentId>
</Person>
</ArrayOfPerson>
在代码中执行XML序列化之后,您可以应用此答案中的RemoveAllNamespaces()函数: 如何使用C#从XML中删除所有名称空间?
This is being added because you're serialising a list of Person
but are using a sub-type of Student
. 这是因为您正在序列化Person
列表但正在使用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
: 如果您不想要这些属性并且只有一种类型的“人”,那么您应该折叠Student
和Person
的定义:
public class Person
{
public string Name { get; set; }
public int StudentId { get; set; }
}
[XmlElement("Person", Type = typeof(Student))]
List<Person> students = new List<Person>();
then remove your include Took me forever to figure this one out 然后删除你的包括我永远把我弄清楚这个
In my case the output was like this:就我而言,output 是这样的:
<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 xmlns 污染了子属性,当属性为空(null)并且 class 中的属性名称被固定时,就会发生这种情况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:因此,如果您想从子属性中删除 xmlns:p2 ,只需将其更改为 false :
[XmlElement(IsNullable = false)]
public string Name { get; set; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.