【问题标题】:Add namespaces to serialized xml将命名空间添加到序列化的 xml
【发布时间】:2011-04-23 16:02:16
【问题描述】:

我在 xml 序列化方面遇到了一个非常讨厌的问题 - 我需要在生成的 xml 文件中添加一些特殊信息:

目前看来

<?xml version="1.0" encoding="iso-8859-1"?>
<ORDER_LIST>
  <ORDER version="1.0" xmlns="http://www.opentrans.org/XMLSchema/1.0">... shortened ...</ORDER>
  <ORDER version="1.0" xmlns="http://www.opentrans.org/XMLSchema/1.0">... shortened ...</ORDER>
  <ORDER version="1.0" xmlns="http://www.opentrans.org/XMLSchema/1.0">... shortened ...</ORDER>
</ORDER_LIST>

但它应该看起来像

<?xml version="1.0" encoding="iso-8859-1"?>
<ORDER_LIST>
  <ORDER xmlns="http://www.opentrans.org/XMLSchema/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opentrans.org/XMLSchema/1.0 openTRANS_ORDER_1_0_all_in_one.xsd" version="1.0" type="standard">... shortened ...</ORDER>
  <ORDER xmlns="http://www.opentrans.org/XMLSchema/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opentrans.org/XMLSchema/1.0 openTRANS_ORDER_1_0_all_in_one.xsd" version="1.0" type="standard">... shortened ...</ORDER>
  <ORDER xmlns="http://www.opentrans.org/XMLSchema/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opentrans.org/XMLSchema/1.0 openTRANS_ORDER_1_0_all_in_one.xsd" version="1.0" type="standard">... shortened ...</ORDER>
</ORDER_LIST>

额外的命名空间 (xmlns:xsi) 和 xsi:schemaLocation / 类型属性应添加到结果中。

其实我的代码是:

[XmlTypeAttribute(Namespace="http://www.opentrans.org/XMLSchema/1.0")]
public class OrderContainer
{
[XmlElementAttribute("ORDER")]
public List<ORDER> orderList;
}

---- snip ----

string xmlString;

XmlWriterSettings settings = new XmlWriterSettings()
{
    Encoding = Encoding.GetEncoding("ISO-8859-1"),
    Indent = true,
    IndentChars = "\t",
    NewLineChars = Environment.NewLine,
    ConformanceLevel = ConformanceLevel.Document,
};

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
var testOrder = new ORDER();

var orderContainer = new OrderContainer();
orderContainer.orderList = new List<ORDER>();
orderContainer.orderList.Add(testOrder);

XmlSerializer serializer = new XmlSerializer(typeof(List<ORDER>), new XmlRootAttribute("ORDER_LIST"));

using (MemoryStream ms = new MemoryStream())
{
    using (XmlWriter writer = XmlTextWriter.Create(ms, settings))
        serializer.Serialize(writer, orderList, ns);

    xmlString = Encoding.ASCII.GetString(ms.ToArray());
}

Console.WriteLine(xmlString);

它工作得非常好 - 除了 ORDER 元素上的命名空间和属性。
背景信息:ORDER 类是从 openTrans 定义 (opentrans_order_1_0_all_in_one.xsd) 创建的。
它已使用 Xsd2Code (Xsd2Code) 翻译成 C# 类。
由于是自动生成的,用属性来装饰类是不容易的——我猜?

感谢您的任何提示! (修改了一些信息)

【问题讨论】:

    标签: c# xml serialization namespaces add


    【解决方案1】:

    我知道回答这个问题有点晚了,但我还是会回答,以防有需要的人偶然发现这个问题。

    我为了添加你需要使用这个类的命名空间:System.Xml.Serialization。 XmlSerializerNamespaces,我在问题代码中看到它已经定义好了。

    以下是我在处理 xCBL Xml Schema 时用来添加命名空间的语法:

    [XmlNamespaceDeclarations]
    public XmlSerializerNamespaces xmlns;  //Defined as the Field of the class you want to serialize
    
    //Defined in the Constructor
    xmlns = new XmlSerializerNamespaces();
    xmlns.Add("core", "rrn:org.xcbl:schemas/xcbl/v4_0/core/core.xsd");
    

    这将在生成的 XML 中添加命名空间,如下所示:

    xmlns:core="rrn:org.xcbl:schemas/xcbl/v4_0/core/core.xsd"
    

    【讨论】:

      【解决方案2】:

      它应该是什么样子:

      <?xml version="1.0" encoding="iso-8859-1"?>
      <ORDER_LIST xmlns:trans="http://www.opentrans.org/XMLSchema/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opentrans.org/XMLSchema/1.0 http://www.opentrans.org/XMLSchema/1.0 openTRANS_ORDER_1_0_all_in_one.xsd" version="1.0" >
        <trans:ORDER type="standard" >... shortened ...</ORDER>
        <trans:ORDER type="standard" >... shortened ...</ORDER>
        <trans:ORDER type="standard" >... shortened ...</ORDER> 
      </ORDER_LIST>
      

      我不熟悉xsd2code,但如果你是手工编码,你需要把属性。

      [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.opentrans.org/XMLSchema/1.0")]
      [System.Xml.Serialization.XmlElementAttribute("Order")]
      public class Order() {
                  String Type
      
              }
      
      [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.opentrans.org/XMLSchema/1.0")]
      [System.Xml.Serialization.XmlRootAttribute("OrderList", Namespace="http://www.opentrans.org/XMLSchema/1.0", IsNullable=false)]
      public class OrderList() {
                List<Orders> 
      
              }
      

      【讨论】:

      • 属性中的信息是否也可以使用“普通”代码放在对象上?
      • Sam,抱歉,您的代码混合了类和属性属性的限定范围。
      【解决方案3】:

      试试这个。它是创建或阅读 opentrans 文档的库! http://opentrans.wordpress.com/

      【讨论】:

        猜你喜欢
        • 2020-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-24
        • 2017-09-21
        • 2011-09-01
        • 2020-09-04
        • 1970-01-01
        相关资源
        最近更新 更多