简体   繁体   English

删除 p2:type="&lt;<type> &gt;" xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" 来自 xml</type>

[英]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 : 如果您不想要这些属性并且只有一种类型的“人”,那么您应该折叠StudentPerson的定义:

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.

相关问题 使用 DataContractSerializer 时删除 xmlns:i=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; - remove xmlns:i=“http://www.w3.org/2001/XMLSchema-instance” when using DataContractSerializer 从名称空间&#39;http://www.w3.org/2001/XMLSchema-instance&#39;期望元素&#39;CustomerLeads&#39; - Expecting element 'CustomerLeads' from namespace 'http://www.w3.org/2001/XMLSchema-instance' 避免在.Net DataContractSerializer中使用“http://www.w3.org/2001/XMLSchema-instance”命名空间 - Avoiding using the “http://www.w3.org/2001/XMLSchema-instance” namespace with .Net DataContractSerializer C#中的XML反序列化错误-InvalidOperationException: <element xmlns='http://www.w3.org/2001/XMLSchema'> 没想到 - XML Deserialization Error in C# - InvalidOperationException: <element xmlns='http://www.w3.org/2001/XMLSchema'> was not expected 具有“ http://www.w3.org/2001/XMLSchema”名称空间的性能影响 - Performance impact of having “http://www.w3.org/2001/XMLSchema” namespace 元素http://www.w3.org/2001/XMLSchema:complexType在此上下文中无效 - Element http://www.w3.org/2001/XMLSchema:complexType is invalid in this context C# XPathSelectElement 和 xml 属性 xmlns=“http://www.w3.org/2000/09/xmldsig#” 帮助 - C# XPathSelectElement and xml with attribute xmlns=“http://www.w3.org/2000/09/xmldsig#” Help XmlDocument.Validate(…)中的“类型&#39;http://www.w3.org/2000/09/xmldsig#:SignatureType&#39;未声明” - “Type 'http://www.w3.org/2000/09/xmldsig#:SignatureType' is not declared” in XmlDocument.Validate(…) JWT Header 算法:“hs256”与“http://www.w3.org/2001/04/xmldsig-more#hmac-sha256”相同 - JWT Header algorithm: is "hs256" the same as "http://www.w3.org/2001/04/xmldsig-more#hmac-sha256" SignedXml CanonicalizationMethod - http://www.w3.org/2006/12/xml-c14n11 - SignedXml CanonicalizationMethod - http://www.w3.org/2006/12/xml-c14n11
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM