如何在 C# 中仅反序列化 XML 文档的一部分 [英] How to deserialize only part of an XML document in C#
问题描述
这是我试图解决的问题的一个虚构示例.如果我在 C# 中工作,并且有这样的 XML:
Here's a fictitious example of the problem I'm trying to solve. If I'm working in C#, and have XML like this:
<?xml version="1.0" encoding="utf-8"?>
<Cars>
<Car>
<StockNumber>1020</StockNumber>
<Make>Nissan</Make>
<Model>Sentra</Model>
</Car>
<Car>
<StockNumber>1010</StockNumber>
<Make>Toyota</Make>
<Model>Corolla</Model>
</Car>
<SalesPerson>
<Company>Acme Sales</Company>
<Position>
<Salary>
<Amount>1000</Amount>
<Unit>Dollars</Unit>
... and on... and on....
</SalesPerson>
</Cars>
SalesPerson 中的 XML 可能很长,大小可达数兆字节.我想反序列化标记,但不反序列化 SalesPerson XML 元素,而是将其保留为原始形式以备后用".
the XML inside SalesPerson can be very long, megabytes in size. I want to deserialize the tag, but not deserialize the SalesPerson XML element instead keeping it in raw form "for later on".
本质上,我希望能够将其用作 XML 的对象表示.
Essentially I would like to be able to use this as a Objects representation of the XML.
[System.Xml.Serialization.XmlRootAttribute("Cars", Namespace = "", IsNullable = false)]
public class Cars
{
[XmlArrayItem(typeof(Car))]
public Car[] Car { get; set; }
public Stream SalesPerson { get; set; }
}
public class Car
{
[System.Xml.Serialization.XmlElementAttribute("StockNumber")]
public string StockNumber{ get; set; }
[System.Xml.Serialization.XmlElementAttribute("Make")]
public string Make{ get; set; }
[System.Xml.Serialization.XmlElementAttribute("Model")]
public string Model{ get; set; }
}
其中 Cars 对象上的 SalesPerson 属性将包含一个带有原始 xml 的流,该流位于 <SalesPerson> 中.通过 XmlSerializer 运行后的 xml 元素.
where the SalesPerson property on the Cars object would contain a stream with the raw xml that is within the <SalesPerson> xml element after being run through an XmlSerializer.
这能做到吗?我可以选择只反序列化 xml 文档的一部分"吗?
Can this be done? Can I choose to only deserialize "part of" an xml document?
谢谢!-迈克
附言从如何反序列化XML文档
推荐答案
这可能是一个有点旧的线程,但无论如何我都会发布.我遇到了同样的问题(需要从超过 1MB 的文件中反序列化 10kb 的数据).在主对象(它有一个需要反序列化的 InnerObject)中,我实现了一个 IXmlSerializable 接口,然后更改了 ReadXml 方法.
It might be a bit old thread, but i will post anyway. i had the same problem (needed to deserialize like 10kb of data from a file that had more than 1MB). In main object (which has a InnerObject that needs to be deserializer) i implemented a IXmlSerializable interface, then changed the ReadXml method.
我们有 xmlTextReader 作为输入,第一行是读取一个 XML 标签:
We have xmlTextReader as input , the first line is to read till a XML tag:
reader.ReadToDescendant("InnerObjectTag"); //tag which matches the InnerObject
然后为我们要反序列化的对象类型创建XMLSerializer并反序列化
Then create XMLSerializer for a type of the object we want to deserialize and deserialize it
XmlSerializer serializer = new XmlSerializer(typeof(InnerObject));
this.innerObject = serializer.Deserialize(reader.ReadSubtree()); //this gives serializer the part of XML that is for the innerObject data
reader.close(); //now skip the rest
这为我节省了大量反序列化时间,并允许我只读取 XML 的一部分(只是描述文件的一些细节,这可能有助于用户确定文件是否是他想要加载的内容).
this saved me a lot of time to deserialize and allows me to read just a part of XML (just some details that describe the file, which might help the user to decide if the file is what he wants to load).
这篇关于如何在 C# 中仅反序列化 XML 文档的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!