我已经定义了一个类型为Example的对象如下所示,实例化并使用XmlSerializer进行序列化后,我得到的是x003A
而不是冒号 :
这是我的代码:
public class Example
{
[XmlElement("Node1")]
public string Node1 { get; set; }
[XmlElement("rd:Node2")]
public string Node2 { get; set; }
}
序列化代码
Example example = new Example { Node1 = "value1", Node2 = "value2" };
XmlSerializerNamespaces namespaceSerializer = new XmlSerializerNamespaces();
namespaceSerializer.Add("rd", @"http://schemas.microsoft.com/SQLServer/reporting/reportdesigner");
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Example));
string path = System.Windows.Forms.Application.StartupPath + "//example.xml";
using (StreamWriter writer = new StreamWriter(path))
{
serializer.Serialize(writer, example, namespaceSerializer);
}
预期结果
<?xml version="1.0" encoding="utf-8"?>
<Example xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Node1>value1</Node1>
<rd:Node2>value2</rd:Node2>
</Example>
实际结果:
<?xml version="1.0" encoding="utf-8"?>
<Example xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Node1>value1</Node1>
<rd_x003A_Node2>value2</rd_x003A_Node2>
</Example>
任何在这方面的帮助和指导都将不胜感激。提前感谢!
rd:
会被解释为XML命名空间。你期望的结果是无效的XML,因为该命名空间未定义。 - Lucas Trzesniewski