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

时间:2018-09-02 04:43:41

标签: c# xml

我有4个不同的名称空间,并按如下所示添加它们

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是没有名称空间的类的示例。我有50多个具有很多属性的类,我想避免在每个属性上都设置一个名称空间。

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

<?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的{​​{1}}的情况下,在序列化期间将名称空间自动设置为Namespace吗?

1 个答案:

答案 0 :(得分:0)

如果要在运行时更改名称空间,则可以使用XmlAttributeOverrides

  

XmlAttributes对象包含一个属性对象的并集   导致XmlSerializer覆盖其默认序列化行为   用于一组对象。您选择要放置在   XmlAttributes对象,具体取决于您想要的特定行为   覆盖。例如,XmlSerializer序列化一个类成员   作为默认的XML元素。如果要将该成员序列化   作为XM属性,您可以创建一个XmlAttributeAttribute,   将其分配给XmlAttributes的XmlAttribute属性,然后添加   XmlAttributes对象更改为XmlAttributeOverrides对象。

     

使用此重载覆盖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);
    }
}