将命名空间添加到序列化的xml

时间:2011-04-23 16:02:17

标签: c# xml serialization namespaces add

我对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 / type属性添加到结果中。

其实我的代码是:

[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#类 由于自动生成,使用属性装饰类是不容易的 - 我猜?

感谢任何提示! (编辑了一些信息)

3 个答案:

答案 0 :(得分:2)

我知道回答这个问题的时间有点迟了,但我仍在回答,以防万一有需要的人偶然发现这个问题。

我要添加使用此类所需的命名空间:System.Xml.Serialization。 XmlSerializerNamespaces,我在问题代码中看到它已被定义。

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

[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"

答案 1 :(得分:1)

它应该是什么样的:

<?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> 

        }

答案 2 :(得分:0)

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