2011-04-23 56 views
0

我得到了与XML序列化一个非常讨厌的问题 - 我需要一些特殊的信息添加到生成的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:schemaLocati on/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)。
它已被翻译成C#类使用Xsd2Code(Xsd2Code)。
由于自动生成,用属性装饰类是不容易的 - 我猜?

感谢您的任何提示! (编辑整理了一些资料)

回答

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> 

我不familar与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> 

     } 
+0

灿来自属性的信息也可以使用“普通”代码放在对象上? – Sascha 2011-04-23 19:22:16

+0

Sam,很抱歉地说,但是你的代码混合了类和属性属性的限定范围。 – Sascha 2011-04-23 20:30:28

1

我知道回答这个问题迟了一点,但尽管如此,我还是在遇到有人需要绊倒这个问题时回答。

我订购你需要使用这个类的命名空间: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"