【问题标题】:XML Serialization NamespacesXML 序列化命名空间
【发布时间】:2014-12-17 16:33:57
【问题描述】:

我的代码生成的命名空间存在问题。我想要的是下面的 XML:

<?xml version="1.0" encoding="utf-8"?>
<ClassToSerialize Type="Customer" Name="Some Name" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.123.org/namespace C:\Schema\ClassToSerialize.xsd" 
xmlns:Test="http://www.Test.org/" xmlns="http://www.nrf-arts.org/namespace">
  <Address>
    <Line1>Addr1</Line1>
    <Line2>Addr2</Line2>
  </Address>
</ClassToSerialize>

我得到的是这个 XML:

<?xml version="1.0" encoding="utf-8"?>
<ClassToSerialize xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:schemaLocation="http://www.123.org/namespace C:\Schema\ClassToSerialize.xsd"
 xmlns:Test="http://www.Test.org/" xmlns:xmlns="http://www.nrf-arts.org/namespace" Type="Customer" Name="Some Name">
  <Address>
    <Line1>Addr1</Line1>
    <Line2>Addr2</Line2>
  </Address>
</ClassToSerialize>

主要区别是:

1. xmlns:schemaLocation= needs to be xsi:schemaLocation=
2. xmlns:xmlns= needs to be xmlns=
3. Attributes Order, I would prefer the Attributes to be presented before the namespace attributes (This is not a big Issue, just nice to have)

目前我正在做的是将上面 1 和 2 中的序列化字符串中的值替换为我想要的值,这是一种可行的 hack,但我怀疑有一种方法可以修改命名空间代码来执行此操作?

这是我正在使用的代码,如何更改 GetNameSpace() 以完成我在第 1 点和第 2 点中需要的操作,我还可以重新排序属性吗?:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        ClassToSerialize myInstance = new ClassToSerialize();
        myInstance.Type = "Customer";
        myInstance.Name = "Some Name";
        myInstance.AddressField = new Address("Addr1", "Addr2");

        String sString = SerializeObject<ClassToSerialize>(myInstance, GetNameSpace());

        //Hack to achieve what I want from namespaces
        sString = sString.Replace("xmlns:schemaLocation=", "xsi:schemaLocation=");
        sString = sString.Replace("xmlns:xmlns=", "xmlns=");
    }

    private XmlSerializerNamespaces GetNameSpace()
    {
        XmlSerializerNamespaces xsNS = new XmlSerializerNamespaces();

        xsNS.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        xsNS.Add("xmlns", "http://www.nrf-arts.org/namespace");
        xsNS.Add("schemaLocation", "http://www.123.org/namespace C:\\Schema\\ClassToSerialize.xsd");
        xsNS.Add("Test", "http://www.Test.org/");

        return xsNS;
    }

    public static string SerializeObject<X>(X toSerialize, XmlSerializerNamespaces xmlNameSpace)
    {
        string strRetVal = "";
        try
        {
            XmlSerializer xmlSerializer = new XmlSerializer(toSerialize.GetType());
            StringWriter textWriter = new StringWriter();

            using (StringWriter writer = new Utf8StringWriter())
            {
                xmlSerializer.Serialize(writer, toSerialize, xmlNameSpace);
                strRetVal = writer.ToString();
            }
        }
        catch (Exception ex)
        {
            string strError = ex.ToString();
        }

        return strRetVal;
    }
}

public class Utf8StringWriter : StringWriter
{
    public override Encoding Encoding
    {
        get { return Encoding.UTF8; }
    }
}

    public class ClassToSerialize
{
    [XmlAttribute()]
    public string Type { get; set; }

    [XmlAttribute()]
    public string Name { get; set; }

    [XmlElement("Address")]
    public Address AddressField { get; set; }
}

public class Address
{
    [XmlElement, DefaultValue("")]
    public string Line1 { get; set; }

    [XmlElement, DefaultValue("")]
    public string Line2 { get; set; }

    public Address()
    {

    }

    public Address(string L1, string L2)
    {
        Line1 = L1;
        Line2 = L2;
    }
}

【问题讨论】:

  • schemaLocation 不是命名空间。

标签: c# xml xml-namespaces


【解决方案1】:
  1. 您可以通过按照How to add xsi schemalocation to root c # object XmlSerializer 中的答案添加合成属性来添加xsi:schemaLocation——但使用属性而不是字段。如果你使用一个字段,你实际上会添加到你的类在内存中的足迹。

  2. 要定义默认命名空间xmlns="http://www.nrf-arts.org/namespace",您可以将[XmlRoot("ClassToSerialize", Namespace="http://www.nrf-arts.org/namespace")] 应用到您的ClassToSerializeallocate an XmlRootAttribute override and pass it to the XmlSerializer constructor。如果您执行后者,请务必缓存序列化程序。

  3. 除了实现IXmlSerializable有点繁琐,不知道能不能用XmlSerializer控制属性顺序。但是,XML 规范声明了"the order of attribute specifications in a start-tag or empty-element tag is not significant",所以我建议不要担心。

因此,以下应该可以解决问题。请注意,我将您的 GetNameSpace() 移动到 ClassToSerialize 中并将其重命名为 GetAdditionalNamespaces()

[XmlRoot("ClassToSerialize", Namespace="http://www.nrf-arts.org/namespace")]
public class ClassToSerialize
{
    public static XmlSerializerNamespaces GetAdditionalNamespaces()
    {
        XmlSerializerNamespaces xsNS = new XmlSerializerNamespaces();

        xsNS.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        xsNS.Add("Test", "http://www.Test.org/");

        return xsNS;
    }

    [XmlAttribute("schemaLocation", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
    public string XSDSchemaLocation
    {
        get
        {
            return "http://www.123.org/namespace C:\\Schema\\ClassToSerialize.xsd";
        }
        set
        {
            // Do nothing - fake property.
        }
    }

    [XmlAttribute()]
    public string Type { get; set; }

    [XmlAttribute()]
    public string Name { get; set; }

    [XmlElement("Address")]
    public Address AddressField { get; set; }
}

【讨论】:

    猜你喜欢
    • 2012-06-17
    • 1970-01-01
    • 2012-11-19
    • 1970-01-01
    • 1970-01-01
    • 2011-01-21
    • 2015-11-03
    • 1970-01-01
    相关资源
    最近更新 更多