C# 将名称空间添加到序列化的xml
我在xml序列化方面遇到了一个非常棘手的问题—我需要向生成的xml文件中添加一些特殊信息: 目前看来,C# 将名称空间添加到序列化的xml,c#,xml,serialization,namespaces,add,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="
<?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);
[XmlTypeAttribute(命名空间=”http://www.opentrans.org/XMLSchema/1.0")]
公共类OrderContainer
{
[XmlElementAttribute(“订单”)]
公共列表订单列表;
}
----剪断----
字符串xmlString;
XmlWriterSettings=新的XmlWriterSettings()
{
Encoding=Encoding.GetEncoding(“ISO-8859-1”),
缩进=真,
IndentChars=“\t”,
NewLineChars=Environment.NewLine,
ConformanceLevel=ConformanceLevel.Document,
};
XmlSerializerNamespaces ns=新的XmlSerializerNamespaces();
加上(“,”);
var testOrder=新订单();
var orderContainer=new orderContainer();
orderContainer.orderList=新列表();
orderContainer.orderList.Add(testOrder);
XmlSerializer serializer=新的XmlSerializer(typeof(List),新的XmlRootAttribute(“ORDER_List”);
使用(MemoryStream ms=new MemoryStream())
{
使用(XmlWriter=XmlTextWriter.Create(ms,设置))
serializer.Serialize(writer、orderList、ns);
xmlString=Encoding.ASCII.GetString(ms.ToArray());
}
Console.WriteLine(xmlString);
除了ORDER元素上的名称空间和属性之外,它工作得非常好。背景信息:ORDER类是根据openTrans定义()创建的。
它已使用Xsd2Code()翻译成C#类。
由于自动生成,用属性修饰类是不容易的——我想是吧 谢谢你的提示强>
(编辑了一些信息)它应该是这样的:
<?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>
... 缩短。。。
... 缩短。。。
... 缩短。。。
我不熟悉XSD2代码,但如果您是手工编码,则需要放置属性
[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>
}
[System.Xml.Serialization.XmlTypeAttribute(命名空间=”http://www.opentrans.org/XMLSchema/1.0")]
[System.Xml.Serialization.xmlementAttribute(“订单”)]
公共秩序{
字符串类型
}
[System.Xml.Serialization.XmlTypeAttribute(命名空间=”http://www.opentrans.org/XMLSchema/1.0")]
[System.Xml.Serialization.XmlRootAttribute(“OrderList”,命名空间=”http://www.opentrans.org/XMLSchema/1.0“,IsNullable=false)]
公共类OrderList(){
列表
}
它的外观应该是:
<?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>
... 缩短。。。
... 缩短。。。
... 缩短。。。
我不熟悉XSD2代码,但如果您是手工编码,则需要放置属性
[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>
}
[System.Xml.Serialization.XmlTypeAttribute(命名空间=”http://www.opentrans.org/XMLSchema/1.0")]
[System.Xml.Serialization.xmlementAttribute(“订单”)]
公共秩序{
字符串类型
}
[System.Xml.Serialization.XmlTypeAttribute(命名空间=”http://www.opentrans.org/XMLSchema/1.0")]
[System.Xml.Serialization.XmlRootAttribute(“OrderList”,命名空间=”http://www.opentrans.org/XMLSchema/1.0“,IsNullable=false)]
公共类OrderList(){
列表
}
试试这个。这是一个创建或阅读opentrans文档的库!
试试这个。这是一个创建或阅读opentrans文档的库!
我知道现在回答这个问题有点晚了,但我还是在回答,以防有需要的人偶然发现这个问题 为了添加名称空间,您需要使用这个类: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"
我知道现在回答这个问题有点晚了,但无论如何,我会在有需要的人偶然发现这个问题时回答 为了添加名称空间,您需要使用这个类: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"
属性中的信息也可以使用“普通”代码放在对象上吗?抱歉,Sam,但是您的代码混合了类属性和属性属性的限定范围。属性中的信息也可以使用“普通”代码放在对象上吗?抱歉,Sam,但是您的代码混合了类和属性属性的限定范围。