代码之家  ›  专栏  ›  技术社区  ›  Ivan-Mark Debono

当属性的命名空间为空时,如何在序列化时设置命名空间?

  •  1
  • Ivan-Mark Debono  · 技术社区  · 6 年前

    var namespaces = new XmlSerializerNamespaces();
    namespaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    namespaces.Add("abc", "urn:abc");
    namespaces.Add(string.Empty, "urn:efg");
    namespaces.Add("xyz", "urn:xyz");
    

    我的对象如下(小示例):

    [XmlRoot(Namespace = "urn:abc")]
    public class RootDocument
    {
        public Header Header { get; set; }
    }
    
    public class Header
    {
        //No namespace here
        public SomeNode SomeNode { get; set; }
    }
    
    public class SomeNode 
    {
        [XmlElement(Namespace = "urn:xyz")]
        public OtherNode { get; set; }
    }
    
    public class OtherNode
    {
        [XmlText]
        public string Value { get; set; }
    }
    

    班级 SomeNode

    当我序列化上述内容时,我得到:

    <?xml version="1.0" encoding="utf-16"?>
    <abc:RootDocument xmlns="urn:efg" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xyz="urn:xyz" xmlns:abc="urn:abc">
        <abc:Header>
            <abc:SomeNode>
                <xyz:OtherNode>true</xyz:OtherNode>
            </abc:SomeNode>
        </abc:Header>
    </abc:RootDocument
    

    但是,输出必须是:

    <?xml version="1.0" encoding="utf-16"?>
    <abc:RootDocument xmlns:efg="urn:efg" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xyz="urn:xyz" xmlns:abc="urn:abc">
        <abc:Header>
            <efg:SomeNode>
                <xyz:OtherNode>true</xyz:OtherNode>
            </efg:SomeNode>
        </abc:Header>
    </abc:RootDocument
    

    我可以在序列化期间自动将命名空间设置为 efg 在所有具有空工作区的节点上(即,属性没有 Namespace XmlElement 设置)?

    1 回复  |  直到 6 年前
        1
  •  0
  •   Barr J    6 年前

    如果您想在运行时更改名称空间,您有 XmlAttributeOverrides :

    XmlAttributes对象包含属性对象的并集 默认情况下作为XML元素。如果要序列化成员 作为XM属性,您可以创建一个XmlAttributeAttribute,

    使用此重载重写XmlRootAttribute或XmlTypeAttribute。

    public class Group
    {
       public string GroupName;
       [XmlAttribute]
       public int GroupCode;
    }
    
      public class Sample
      {
        public XmlSerializer CreateOverrider()
        {
           // Create an XmlAttributeOverrides object. 
           XmlAttributeOverrides xOver = new XmlAttributeOverrides();
    
           /* Create an XmlAttributeAttribute to override the base class
           object's XmlAttributeAttribute object. Give the overriding object
           a new attribute name ("Code"). */
           XmlAttributeAttribute xAtt = new XmlAttributeAttribute();
           xAtt.AttributeName = "Code";
    
           /* Create an instance of the XmlAttributes class and set the 
           XmlAttribute property to the XmlAttributeAttribute object. */
           XmlAttributes attrs = new XmlAttributes();
           attrs.XmlAttribute = xAtt;
    
           /* Add the XmlAttributes object to the XmlAttributeOverrides
              and specify the type and member name to override. */
           xOver.Add(typeof(Group), "GroupCode", attrs);
    
           XmlSerializer xSer = new XmlSerializer(typeof(Group), xOver);
           return xSer;
        }
      }
    

    还有一个,用 XmlSerializerNamespaces :

    [XmlRoot("Node", Namespace="http://somenamepsace")]
    public class MyType {
        [XmlElement("childNode")]
        public string Value { get; set; }
    }
    
    static class Program
    {
        static void Main()
        {
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("myNamespace", "http://somenamepsace");
            XmlSerializer xser = new XmlSerializer(typeof(MyType));
            xser.Serialize(Console.Out, new MyType(), ns);
        }
    }