C# 中的 Xml 序列化
我正在尝试遵循有关 XML 序列化的 microsoft 教程,但我遇到一些问题!
这是 XML 文件,用作输入:
<?xml version="1.0" encoding="utf-8"?>
<Books xmlns:books="http://www.cpandl.com" xmlns:money="http://www.cohowinery.com">
<money:Book>
<books:TITLE>A Book Title</books:TITLE>
<money:PRICE books:currency="US Dollar">
<money:price>9.95</money:price>
</money:PRICE>
</money:Book>
</Books>
这是绑定 XML 的类:
public class OrderedItem
{
[XmlElement(Namespace = "http://www.cpandl.com")]
public string ItemName;
[XmlElement(Namespace = "http://www.cpandl.com")]
public string Description;
[XmlElement(Namespace = "http://www.cohowinery.com")]
public decimal UnitPrice;
[XmlElement(Namespace = "http://www.cpandl.com")]
public int Quantity;
[XmlElement(Namespace = "http://www.cohowinery.com")]
public decimal LineTotal;
// A custom method used to calculate price per item.
public void Calculate()
{
LineTotal = UnitPrice * Quantity;
}
}
该函数将 XML 读入“OrderedItem”类:
Console.WriteLine("Reading with Stream");
// Create an instance of the XmlSerializer.
var serializer = new XmlSerializer(typeof(OrderedItem));
// Reading the XML document requires a FileStream.
Stream reader = new FileStream(filename, FileMode.Open);
// Declare an object variable of the type to be deserialized.
// Call the Deserialize method to restore the object's state.
var i = (OrderedItem)serializer.Deserialize(reader);
Console.SetOut(new StreamWriter("a_output.xml"));
serializer.Serialize(Console.Out, i);
这是读取并重写后的 XML:
<?xml version="1.0" encoding="utf-8"?>
<OrderedItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ItemName xmlns="http://www.cpandl.com">Widget</ItemName>
<Description xmlns="http://www.cpandl.com">Regular Widget</Description>
<UnitPrice xmlns="http://www.cohowinery.com">2.3</UnitPrice>
<Quantity xmlns="http://www.cpandl.com">10</Quantity>
<LineTotal xmlns="http://www.cohowinery.com">23</LineTotal>
</OrderedItem>
如您所见,命名空间已扩展。我应该如何编写输出,以获得带有命名空间标签的相同 XML,而不是 uri?
I'm trying to follow a microsoft tutorial about XML serialization, but I getting some problems!!
This is XML file, used as input:
<?xml version="1.0" encoding="utf-8"?>
<Books xmlns:books="http://www.cpandl.com" xmlns:money="http://www.cohowinery.com">
<money:Book>
<books:TITLE>A Book Title</books:TITLE>
<money:PRICE books:currency="US Dollar">
<money:price>9.95</money:price>
</money:PRICE>
</money:Book>
</Books>
This is the class to bind the XML:
public class OrderedItem
{
[XmlElement(Namespace = "http://www.cpandl.com")]
public string ItemName;
[XmlElement(Namespace = "http://www.cpandl.com")]
public string Description;
[XmlElement(Namespace = "http://www.cohowinery.com")]
public decimal UnitPrice;
[XmlElement(Namespace = "http://www.cpandl.com")]
public int Quantity;
[XmlElement(Namespace = "http://www.cohowinery.com")]
public decimal LineTotal;
// A custom method used to calculate price per item.
public void Calculate()
{
LineTotal = UnitPrice * Quantity;
}
}
This function read the XML into 'OrderedItem' class:
Console.WriteLine("Reading with Stream");
// Create an instance of the XmlSerializer.
var serializer = new XmlSerializer(typeof(OrderedItem));
// Reading the XML document requires a FileStream.
Stream reader = new FileStream(filename, FileMode.Open);
// Declare an object variable of the type to be deserialized.
// Call the Deserialize method to restore the object's state.
var i = (OrderedItem)serializer.Deserialize(reader);
Console.SetOut(new StreamWriter("a_output.xml"));
serializer.Serialize(Console.Out, i);
This is the XML after read and rewritten:
<?xml version="1.0" encoding="utf-8"?>
<OrderedItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ItemName xmlns="http://www.cpandl.com">Widget</ItemName>
<Description xmlns="http://www.cpandl.com">Regular Widget</Description>
<UnitPrice xmlns="http://www.cohowinery.com">2.3</UnitPrice>
<Quantity xmlns="http://www.cpandl.com">10</Quantity>
<LineTotal xmlns="http://www.cohowinery.com">23</LineTotal>
</OrderedItem>
As you can see, the namespace are expanded. How should I write the output, to obtain the same XML with namespace label, instead of uri?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
查看
XmlSerializerNameSpaces
类: http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializernamespaces.aspx。这个示例代码应该可以解决问题:
Check out the
XmlSerializerNameSpaces
class: http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializernamespaces.aspx.This example code should do the trick:
您需要添加类型为
XmlSerializerNamespaces< 的成员/code>
,标有
XmlNamespaceDeclarationsAttribute
:然后在序列化时添加命名空间声明:
You need to add a member of type
XmlSerializerNamespaces
, marked with aXmlNamespaceDeclarationsAttribute
:And then add the namespace declarations when serializing:
您可能想查看用于序列化对象的重载方法:
默认序列化< /a>
定义序列化命名空间
正如那里提到的,您可以定义 < code>XmlSerializerNamespaces 包含以下代码行:
希望有帮助。
You may want to have a look on the overloaded method for serializing an object:
default serialization
defining namespaces for serialization
As mentioned there, you can define
XmlSerializerNamespaces
with the following code lines:Hope that helps.
为了将您的对象序列化为上面提供的格式,您必须在您的对象上实现
IXmlSerialized
接口 (MSDN 文档)。该接口允许您实现一些方法,使您能够完全控制类的序列化结果(并将 xml 反序列化回对象)。此主题也已在此处讨论,请在此处查看更多信息:实现 IXmlSerialized 的正确方法
In order to serialize your object to the format provided above you you are going to have to implement the
IXmlSerializable
interface on your object (MSDN Documentation). This interface allows you to implement methods that will give you complete control over the serialized result of your class (and also deserializing the xml back to your object).This topic has been discussed here as well, look here for more information: Proper way to implement IXmlSerializable