XML序列化和XML名称空间-Dodgy .net serializer?

时间:2018-07-06 05:40:30

标签: xml-serialization xml-namespaces

我想对以下对象进行XML序列化。

[System.SerializableAttribute()]
[XmlTypeAttribute(TypeName = "RootNode", Namespace = "http://fax.com/outer")]
public partial class ParentClass
{
    [XmlElementAttribute(Form = XmlSchemaForm.Unqualified, Order = 0)]
    public ZFirstChild FirstChild { get; set; }


    [XmlElementAttribute(Form = XmlSchemaForm.Unqualified, Order = 1)]
    public ZSecondChild SecondChild { get; set; }
}

请注意,我已经为根元素分配了名称空间“ http://fax.com/outer”。

在ZFirstChild内,我有一个ZWrapperElement:

[System.SerializableAttribute()]
public partial class ZFirstChild
{
    //more properties

    [XmlElementAttribute(Form = XmlSchemaForm.Unqualified, Order = 3)]
    public ZWrapperElement WrapperElement { get; set; }
}

ZWrapperElement的定义如下:

[System.SerializableAttribute()]
[XmlTypeAttribute]
public partial class ZWrapperElement
{
    [XmlElementAttribute(Order = 0)]
    public ZAddress Address { get; set; }



    [XmlElementAttribute(Order = 1)]
    public ZCustomer Customer { get; set; }

}

我为ZAddress和ZCustomer元素赋予了它们自己的名称空间,与根名称空间不同。

[System.SerializableAttribute()]
[XmlTypeAttribute(Namespace = "http://example.org/inner")]
public partial class ZAddress
{

    [XmlElementAttribute(Form = XmlSchemaForm.Unqualified, Order = 0)]
    public string StreetNumber { get; set; }

    [XmlElementAttribute(Form = XmlSchemaForm.Unqualified, Order = 1)]
    public string StreetNameName { get; set; }

    [XmlElementAttribute(Form = XmlSchemaForm.Unqualified, Order = 2)]
    public string TelephoneNumber { get; set; }
}


[System.SerializableAttribute()]
[XmlTypeAttribute(Namespace = "http://example.org/inner")]
public partial class ZCustomer
{

    [XmlElementAttribute(Form = XmlSchemaForm.Unqualified, Order = 0)]
    public string Name { get; set; }


    [XmlElementAttribute(Form = XmlSchemaForm.Unqualified, Order = 1)]
    public string Country { get; set; }

}

然后我使用以下序列化代码:

ParentClass typedObj = initTheObject(); //load dummy data.

XmlSerializer xSrz = new XmlSerializer(typeof(ParentClass));

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("ns", "http://fax.com/outer");
ns.Add("n0", "http://example.org/inner");

//removes <?xml version="1.0"
XmlWriterSettings xSettings = new XmlWriterSettings()
{
    Indent = true,
    IndentChars = "  ",
    OmitXmlDeclaration = true
};

string xmlAsStr = "";
StringWriter stringW = new StringWriter(); 
using (XmlWriter xWriter = XmlWriter.Create(stringW, xSettings))
{
    xSrz.Serialize(xWriter, typedObj, ns);
    xWriter.Flush();
    xmlAsStr = stringW.ToString(); // result XML

}

生成的XML是

<RootNode xmlns:ns="http://fax.com/outer" xmlns:n0="http://example.org/inner">
  <FirstChild>
    <ID>123</ID>
    <Name>jane citizen</Name>
    <DOB>2018-07-06T14:58:49.2268315+10:00</DOB>
    <WrapperElement>
      <ns:Address>
        <StreetNumber>101</StreetNumber>
        <StreetNameName>Parra Rd</StreetNameName>
        <TelephoneNumber>04 123 456</TelephoneNumber>
      </ns:Address>
      <ns:Customer>
        <Name>big customer</Name>
        <Country>US of A</Country>
      </ns:Customer>
    </WrapperElement>
  </FirstChild>
  <SecondChild>
    <SeverityCode>456</SeverityCode>
    <Note>test note</Note>
  </SecondChild>
</RootNode>

问题/我的问题

在类定义中,我清楚地将名称空间“ http://fax.com/outer”分配给根元素,并将名称空间“ http://example.org/inner”分配给内部元素ZAddress和ZCustomer;

在序列化中,我已经分配了前缀

ns.Add(“ ns”,“ http://fax.com/outer”); ns.Add(“ n0”,“ http://example.org/inner”);

但是,当我序列化时,我得到了元素

  

<ns:Address> and <ns:Customer>

,这是错误的名称空间!我希望使用前缀<n0:Address> and <n0:Customer>

这是怎么回事?

0 个答案:

没有答案