首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将命名空间添加到序列化的xml中

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

Stack Overflow用户
提问于 2011-04-23 16:02:16
回答 3查看 6.1K关注 0票数 0

在xml序列化方面,我遇到了一个非常棘手的问题--我需要向结果的xml-文件中添加一些特殊信息:

目前,看起来像是

代码语言:javascript
复制
<?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>

,但它应该看起来像

代码语言:javascript
复制
<?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属性添加到结果中。

实际上我的代码是:

代码语言:javascript
复制
[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 (one.xsd)创建的。

它已经被使用C# ( Xsd2Code,Xsd2Code)翻译成了Xsd2Code类。

由于自动生成,用属性装饰类是不容易的-我猜?

谢谢你的提示!(编辑了一些信息)

EN

回答 3

Stack Overflow用户

发布于 2014-02-05 09:53:47

我知道现在回答这个问题为时已晚,但我仍在回答,以防有需要的人在这个问题上绊倒。

我命令添加名称空间,您需要使用这个类: System.Xml.Serialization。XmlSerializerNamespaces,我在已定义的问题代码中看到了这一点。

下面是我在处理xCBL Xml时添加名称空间时使用的语法:

代码语言:javascript
复制
[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中添加名称空间,如下所示:

代码语言:javascript
复制
xmlns:core="rrn:org.xcbl:schemas/xcbl/v4_0/core/core.xsd"
票数 2
EN

Stack Overflow用户

发布于 2011-04-23 17:14:10

应该是这样的:

代码语言:javascript
复制
<?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,但是如果您是手工编码,则需要放置属性。

代码语言:javascript
复制
[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> 

        }
票数 1
EN

Stack Overflow用户

发布于 2013-02-07 12:58:33

尝尝这个。这是一本用来创建或阅读opentrans文档的字典!http://opentrans.wordpress.com/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5765266

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档