简体   繁体   English

没有XML属性节点的C#中的XML序列化

[英]XML Serialization in C# without XML attribute nodes

I have an XML document format from a legacy system that I have to support in a future application. 我有一个来自旧系统的XML文档格式,将来的应用程序必须支持该格式。 I want to be able to both serialize and deserialize the XML between XML and C# objects, however, using the objects generated by xsd.exe, the C# serialization includes the xmlns:xsi..., xsi:... etc XML attributes on the root element of the document that gets generated. 我希望能够在XML和C#对象之间对XML进行序列化和反序列化,但是,使用xsd.exe生成的对象,C#序列化包括xmlns:xsi ...,xsi:...等XML属性生成的文档的根元素。 Is there anyway to disable this so that absolutely no XML attribute nodes get put out in the resulting XML ? 无论如何,有没有禁用它,以便在生成的XML中绝对不会出现XML属性节点? The XML document should be elements only. XML文档应仅是元素。


Duplicate? 重复? XmlSerializer: remove unnecessary xsi and xsd namespaces XmlSerializer:删除不必要的xsi和xsd命名空间

Yes, use the XmlSerializerNamespaces class. 是的,使用XmlSerializerNamespaces类。

Example: 例:

  var s= new System.Xml.Serialization.XmlSerializer(typeof(TypeToSerialize));
  var ns= new System.Xml.Serialization.XmlSerializerNamespaces();
  ns.Add( "", "");
  System.IO.StreamWriter writer= System.IO.File.CreateText(filePath);
  s.Serialize(writer, objectToSerialize, ns);
  writer.Close();

See also: XmlSerializer: remove unnecessary xsi and xsd namespaces 另请参阅: XmlSerializer:删除不必要的xsi和xsd命名空间

There is no way to force XML Serializer to ignore xsi attributes (unless you implement IXmlSerializable and force custom serialization or use XmlAttributeOverrides ). 没有强制XML序列化程序忽略xsi属性的方法(除非您实现IXmlSerializable并强制自定义序列化或使用XmlAttributeOverrides )。 However the only time xsi: attributes show up is when you have a nullable element. 但是,只有当您具有可为空的元素时,才会显示xsi:属性。 If you do need to use nullable elements you can of course post-process the XML to remove all xsi: occurences. 如果确实需要使用可为空的元素,那么您当然可以对XML进行后处理,以删除所有xsi:事件。 However if you do this think about how you will deserialize the XML back into an object, if xsi:nil is missing on an element and the element is defined as a nullable integer you will run into an exception. 但是,如果您这样做,将考虑如何将XML反序列化为对象,如果元素上缺少xsi:nil且该元素被定义为可为空的整数,则会遇到异常。

@Cheeso, please correct me if i am wrong. @Cheeso,如果我错了,请纠正我。

I have the following code. 我有以下代码。

  public class TestSer
    {
        public int? MyProperty { get; set; }   
    }





    TestSer ser = new TestSer();
    ser.MyProperty = null;

    StringBuilder bldr = new StringBuilder();
    var ns = new System.Xml.Serialization.XmlSerializerNamespaces();
    ns.Add("", "");
    XmlSerializer s = new XmlSerializer(typeof(TestSer));
    using (StringWriter writer = new StringWriter(bldr))
    {
        s.Serialize(writer, ser, ns);
    }

I get the following output. 我得到以下输出。

<?xml version="1.0" encoding="utf-16"?>
<TestSer>
  <MyProperty d2p1:nil="true" xmlns:d2p1="http://www.w3.org/2001/XMLSchema-instance" />
</TestSer>

This isn't exactly element only as the question asks for. 这不仅仅是问题所要求的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM