响应您在编辑后看到的'空格',这是因为您使用的编码(Unicode,每个字符2个字节)。
将类序列化为XML并包括CDATA节的问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将类序列化为XML并包括CDATA节的问题相关的知识,希望对你有一定的参考价值。
我有一个类,该类被序列化为XML以供Web服务使用。在此类实例中,XML必须包含Web服务的CDATA部分才能读取它,但我对如何实现这一点感到困惑。
XML需要看起来像:
<UpdateOrderStatus> <Action>2</Action> <Value> <![CDATA[ <Shipment> <Header> <SellerID> ... ]]> </Value> </UpdateOrderStatus>
我能够生成适当的XML,除了CDATA部分。
我的班级结构看起来像:
public class UpdateOrderStatus { public int Action { get; set; } public ValueInfo Value { get; set; } public UpdateOrderStatus() { Value = new ValueInfo(); } public class ValueInfo { public ShipmentInfo Shipment { get; set; } public ValueInfo() { Shipment = new ShipmentInfo(); } public class ShipmentInfo { public PackageListInfo PackageList { get; set; } public HeaderInfo Header { get; set; } public ShipmentInfo() { PackageList = new PackageListInfo(); Header = new HeaderInfo(); } ....
我已经看到了一些使用建议:
[XmlElement("node", typeof(XmlCDataSection))]
但是会导致异常
我也尝试过
[XmlElement("Value" + "<![CDATA[")]
但是结果XML显示不正确
<Value_x003C__x0021__x005B_CDATA_x005B_> .... </Value_x003C__x0021__x005B_CDATA_x005B_>
有人可以告诉我我做错了什么,或者我需要在哪里解决?
-编辑-
在大多数情况下,使每个carlosfigueira可序列化的shippingInfo工作,但是我得到了额外的收益?结果XML中的字符(有关详细信息,请参见文章Writing an XML fragment using XmlWriterSettings and XmlSerializer is giving an extra character)
因此,我将Write XML方法更改为:
public void WriteXml(XmlWriter writer) { using (MemoryStream ms = new MemoryStream()) { XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); XmlWriterSettings settings = new XmlWriterSettings(); settings.OmitXmlDeclaration = true; settings.Encoding = new UnicodeEncoding(bigEndian: false, byteOrderMark: false); settings.Indent = true; using (XmlWriter innerWriter = XmlWriter.Create(ms, settings)) { shipmentInfoSerializer.Serialize(innerWriter, this.Shipment,ns); innerWriter.Flush(); writer.WriteCData(Encoding.UTF8.GetString(ms.ToArray())); } } }
但是我没有例外:
System.InvalidOperationException: There was an error generating the XML document. ---> System.ArgumentException: '.', hexadecimal value 0x00, is an invalid character.
-编辑-
该异常是由于包含了我以前的serializeToString方法引起的。由于删除CDATA输出是正确的(除了空格问题),所以我还得到了应由指定的XML设置删除的名称空间和xml声明。输出为:
<?xml version="1.0"?> <UpdateOrderStatus xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Action>1</Action> <Value><![CDATA[< S h i p m e n t I n f o > < P a c k a g e L i s t > < P a c k a g e > < S h i p D a t e > 2 0 1 2 - 0 7 - 1 3 T 1 1 : 5 8 : 5 1 . 0 9 2 5 6 1 5 - 0 4 : 0 0 < / S h i p D a t e > < I t e m L i s t > < I t e m > < S h i p p e d Q t y > 0 < / S h i p p e d Q t y > < / I t e m > < / I t e m L i s t > < / P a c k a g e > < / P a c k a g e L i s t > < H e a d e r > < S e l l e r I d > S h i p m e n t h e a d e r < / S e l l e r I d > < S O N u m b e r > 0 < / S O N u m b e r > < / H e a d e r > < / S h i p m e n t I n f o > ]]></Value> </UpdateOrderStatus>
关于使用新类避免BOM的任何想法?
-编辑3-成功!
我已经实现了下面建议的更改,现在具有以下writer类和测试方法:
UpdateOrderStatus obj = new UpdateOrderStatus(); obj.Action = 1; obj.Value = new UpdateOrderStatus.ValueInfo(); obj.Value.Shipment = new UpdateOrderStatus.ValueInfo.ShipmentInfo(); obj.Value.Shipment.Header.SellerId = "Shipment header"; obj.Value.Shipment.PackageList = new UpdateOrderStatus.ValueInfo.ShipmentInfo.PackageListInfo(); obj.Value.Shipment.PackageList.Package = new UpdateOrderStatus.ValueInfo.ShipmentInfo.PackageListInfo.PackageInfo(); obj.Value.Shipment.PackageList.Package.ShipDate = DateTime.Now; XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); XmlWriterSettings settings = new XmlWriterSettings(); settings.OmitXmlDeclaration = true; settings.Encoding = new UTF8Encoding(false); settings.Indent = true; XmlSerializer xs = new XmlSerializer(typeof(UpdateOrderStatus)); MemoryStream ms = new MemoryStream(); XmlWriter writer = XmlWriter.Create(ms, settings); xs.Serialize(writer, obj, ns); Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray())); } public void WriteXml(XmlWriter writer) { XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); XmlWriterSettings settings = new XmlWriterSettings(); settings.OmitXmlDeclaration = true; settings.Indent = true; StringBuilder sb = new StringBuilder(); using (XmlWriter innerWriter = XmlWriter.Create(sb, settings)) { shipmentInfoSerializer.Serialize(innerWriter, this.Shipment, ns); innerWriter.Flush(); writer.WriteCData(sb.ToString()); } }
这将产生以下XML:
<UpdateOrderStatus>
<Action>1</Action>
<Value><![CDATA[<ShipmentInfo>
<PackageList>
<Package>
<ShipDate>2012-07-13T14:05:36.6170802-04:00</ShipDate>
<ItemList>
<Item>
<ShippedQty>0</ShippedQty>
</Item>
</ItemList>
</Package>
</PackageList>
<Header>
<SellerId>Shipment header</SellerId>
<SONumber>0</SONumber>
</Header>
</ShipmentInfo>]]></Value>
</UpdateOrderStatus>
我有一个类,该类被序列化为XML以供Web服务使用。在此类实例中,XML必须包含Web服务的CDATA部分才能读取它,但我对如何...... >>>
尝试:
settings.Encoding = new Utf8Encoding(false);
编辑:
此外,请注意MemoryStream
的格式为not不一定是有效的UTF-8编码字符串!您可以使用StringBuilder
而不是MemoryStream
创建内部编写器。 public void WriteXml(XmlWriter writer)
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Indent = true;
StringBuilder sb = new StringBuilder();
using (XmlWriter innerWriter = XmlWriter.Create(sb, settings))
{
shipmentInfoSerializer.Serialize(innerWriter, this.Shipment,ns);
innerWriter.Flush();
writer.WriteCData(sb.ToString());
}
}
对您有帮助吗?http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.createcdatasection.aspx
//Create a CData section.
XmlCDataSection CData;
CData = doc.CreateCDataSection("All Jane Austen novels 25% off starting 3/23!");
//Add the new node to the document.
XmlElement root = doc.DocumentElement;
root.AppendChild(CData);
Console.WriteLine("Display the modified XML...");
doc.Save(Console.Out);
而且,使用该属性时,您得到什么异常?
-编辑-
您可以尝试添加自定义类,然后执行以下操作:
some xml serializable class, { ....... [XmlElement("PayLoad", Type=typeof(CDATA))] public CDATA PayLoad { get { return _payLoad; } set { _payLoad = value; } } } public class CDATA : IXmlSerializable { private string text; public CDATA() {} public CDATA(string text) { this.text = text; } public string Text { get { return text; } } /// <summary> /// Interface implementation not used here. /// </summary> XmlSchema IXmlSerializable.GetSchema() { return null; } /// <summary> /// Interface implementation, which reads the content of the CDATA tag /// </summary> void IXmlSerializable.ReadXml(XmlReader reader) { this.text = reader.ReadElementString(); } /// <summary> /// Interface implementation, which writes the CDATA tag to the xml /// </summary> void IXmlSerializable.WriteXml(XmlWriter writer) { writer.WriteCData(this.text); } }
如此处http://bytes.com/topic/net/answers/530724-cdata-xmltextattribute
将ShipmentInfo作为IXmlSerializable
类型实现将接近您所需要的-请参见下面的示例。
public class StackOverflow_11471676
{
public class UpdateOrderStatus
{
public int Action { get; set; }
public ValueInfo Value { get; set; }
}
[XmlType(TypeName = "Shipment")]
public class ShipmentInfo
{
public string Header { get; set; }
public string Body { get; set; }
}
public class ValueInfo : IXmlSerializable
{
public ShipmentInfo Shipment { get; set; }
private XmlSerializer shipmentInfoSerializer = new XmlSerializer(typeof(ShipmentInfo));
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
public void ReadXml(XmlReader reader)
{
using (MemoryStream ms = new MemoryStream(
Encoding.UTF8.GetBytes(
reader.ReadContentAsString())))
{
Shipment = (ShipmentInfo)this.shipmentInfoSerializer.Deserialize(ms);
}
}
public void WriteXml(XmlWriter writer)
{
using (MemoryStream ms = new MemoryStream())
{
using (XmlWriter innerWriter = XmlWriter.Create(ms, new XmlWriterSettings { OmitXmlDeclaration = true }))
{
shipmentInfoSerializer.Serialize(innerWriter, this.Shipment);
innerWriter.Flush();
writer.WriteCData(Encoding.UTF8.GetString(ms.ToArray()));
}
}
}
}
public static void Test()
{
UpdateOrderStatus obj = new UpdateOrderStatus
{
Action = 1,
Value = new ValueInfo
{
Shipment = new ShipmentInfo
{
Header = "Shipment header",
Body = "Shipment body"
}
}
};
XmlSerializer xs = new XmlSerializer(typeof(UpdateOrderStatus));
MemoryStream ms = new MemoryStream();
xs.Serialize(ms, obj);
Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()));
}
}
下面的示例仅在定义了架构的结构并且您没有选择更改架构的选择时。
当您对[xmltext]]进行反序列化/序列化时,很难将文本保存在CDATA[]
中括。您可以使用compiletransform照原样获取CDATA
中的xml
值,但是一旦在C#中反序列化并加载到内存中,CDATA
就会丢失。
这是最简单的方法之一
- 反序列化/序列化
- 一旦获得最终的xml输出。最终的xml可以是转换为字符串并按如下所示进行解析,然后将其返回字符串将
CDATA
嵌入到test1值。string xml ="<test><test1>@#@!#!#!@#!@#%$%@#$%#$%</test1></test>"; XNamespace ns = @""; XDocument doc = XDocument.Parse(xml); string xmlString = string.Empty; var coll = from query in doc.Descendants(ns + "test1") select query; foreach (var value in coll){ value.ReplaceNodes(new XCData(value .Value)); } doc.save("test.xml");// convert doc.tostring()
我认为carlosfigueira给出了一个很好的答案,但乍一看可能有点难以理解。这是您考虑的替代方法,可以分别对 将您的业务对象类定义为:UpdateOrderStatus
和ShipmentInfo
进行序列化/反序列化。
[XmlRoot("UpdateOrderStatus")]
public class UpdateOrderStatus
{
[XmlElement("Action")]
public int Action { get; set; }
private String valueField;
[XmlElement("Value")]
public XmlCDataSection Value
{
get
{
XmlDocument xmlDoc = new XmlDocument();
return xmlDoc.CreateCDataSection(valueField);
}
set
{
this.valueField = value.Value;
}
}
[XmlIgnore]
public ShipmentInfo Shipment
{
get;
set;
}
}
[XmlRoot("ShipmentInfo")]
public class ShipmentInfo
{
[XmlElement("Package")]
public String Package { get; set; }
[XmlElement("Header")]
public String Header { get; set; }
}
注意,XmlCDataSection
字段应使用Value
。这是测试/帮助功能:
// Test function
const string t = @"<UpdateOrderStatus>
<Action>2</Action>
<Value>
<![CDATA[<ShipmentInfo>
<Package>packageInfo</Package>
<Header>headerInfo</Header>
</ShipmentInfo>]]>
</Value>
</UpdateOrderStatus>";
static void Test1()
{
UpdateOrderStatus os = Deserialize(t);
String t2 = XmlUtil.Serialize(os);
}
// Helper functions
static UpdateOrderStatus Deserialize(String str)
{
UpdateOrderStatus os = XmlUtil.DeserializeString<UpdateOrderStatus>(str);
os.Shipment = XmlUtil.DeserializeString<ShipmentInfo>(os.Value.InnerText);
return os;
}
public static class XmlUtil
{
public static String Serialize<T>(T o)
{
XmlSerializer s = new XmlSerializer(typeof(T)); //, overrides);
//StringBuilder builder = new StringBuilder();
MemoryStream ms = new MemoryStream();
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = Encoding.UTF8;
settings.Indent = true;
using (XmlWriter xmlWriter = XmlWriter.Create(ms, settings))
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add(String.Empty, String.Empty);
s.Serialize(xmlWriter, o, ns);
}
Byte[] bytes = ms.ToArray();
// discard the BOM part
Int32 idx = settings.Encoding.GetPreamble().Length;
return Encoding.UTF8.GetString(bytes, idx, bytes.Length - idx);
}
public static T DeserializeString<T>(String content)
{
using (TextReader reader = new StringReader(content))
{
XmlSerializer s = new XmlSerializer(typeof(T));
return (T)s.Deserialize(reader);
}
}
...
}
响应您在编辑后看到的'空格',这是因为您使用的编码(Unicode,每个字符2个字节)。
对您有帮助吗?http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.createcdatasection.aspx
//Create a CData section.
XmlCDataSection CData;
CData = doc.CreateCDataSection("All Jane Austen novels 25% off starting 3/23!");
//Add the new node to the document.
XmlElement root = doc.DocumentElement;
root.AppendChild(CData);
Console.WriteLine("Display the modified XML...");
doc.Save(Console.Out);
将ShipmentInfo作为IXmlSerializable
类型实现将接近您所需要的-请参见下面的示例。
public class StackOverflow_11471676
{
public class UpdateOrderStatus
{
public int Action { get; set; }
public ValueInfo Value { get; set; }
}
[XmlType(TypeName = "Shipment")]
public class ShipmentInfo
{
public string Header { get; set; }
public string Body { get; set; }
}
public class ValueInfo : IXmlSerializable
{
public ShipmentInfo Shipment { get; set; }
private XmlSerializer shipmentInfoSerializer = new XmlSerializer(typeof(ShipmentInfo));
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
public void ReadXml(XmlReader reader)
{
using (MemoryStream ms = new MemoryStream(
Encoding.UTF8.GetBytes(
reader.ReadContentAsString())))
{
Shipment = (ShipmentInfo)this.shipmentInfoSerializer.Deserialize(ms);
}
}
public void WriteXml(XmlWriter writer)
{
using (MemoryStream ms = new MemoryStream())
{
using (XmlWriter innerWriter = XmlWriter.Create(ms, new XmlWriterSettings { OmitXmlDeclaration = true }))
{
shipmentInfoSerializer.Serialize(innerWriter, this.Shipment);
innerWriter.Flush();
writer.WriteCData(Encoding.UTF8.GetString(ms.ToArray()));
}
}
}
}
public static void Test()
{
UpdateOrderStatus obj = new UpdateOrderStatus
{
Action = 1,
Value = new ValueInfo
{
Shipment = new ShipmentInfo
{
Header = "Shipment header",
Body = "Shipment body"
}
}
};
XmlSerializer xs = new XmlSerializer(typeof(UpdateOrderStatus));
MemoryStream ms = new MemoryStream();
xs.Serialize(ms, obj);
Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()));
}
}
下面的示例仅在定义了架构的结构并且您没有选择更改架构的选择时。
当您对[xmltext]]进行反序列化/序列化时,很难将文本保存在CDATA[]
中括。您可以使用compiletransform照原样获取CDATA
中的xml
值,但是一旦在C#中反序列化并加载到内存中,CDATA
就会丢失。
我认为carlosfigueira给出了一个很好的答案,但乍一看可能有点难以理解。这是您考虑的替代方法,可以分别对 将您的业务对象类定义为:UpdateOrderStatus
和ShipmentInfo
进行序列化/反序列化。
以上是关于将类序列化为XML并包括CDATA节的问题的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Windows Phone 8 SDK 将类序列化为 XML?