Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to deserialize only part of an XML document in C#
    text
    copied!<p>Here's a fictitious example of the problem I'm trying to solve. If I'm working in C#, and have XML like this:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;Cars&gt; &lt;Car&gt; &lt;StockNumber&gt;1020&lt;/StockNumber&gt; &lt;Make&gt;Nissan&lt;/Make&gt; &lt;Model&gt;Sentra&lt;/Model&gt; &lt;/Car&gt; &lt;Car&gt; &lt;StockNumber&gt;1010&lt;/StockNumber&gt; &lt;Make&gt;Toyota&lt;/Make&gt; &lt;Model&gt;Corolla&lt;/Model&gt; &lt;/Car&gt; &lt;SalesPerson&gt; &lt;Company&gt;Acme Sales&lt;/Company&gt; &lt;Position&gt; &lt;Salary&gt; &lt;Amount&gt;1000&lt;/Amount&gt; &lt;Unit&gt;Dollars&lt;/Unit&gt; ... and on... and on.... &lt;/SalesPerson&gt; &lt;/Cars&gt; </code></pre> <p>the XML inside SalesPerson can be very long, megabytes in size. I want to deserialize the tag, <strong>but</strong> not deserialize the SalesPerson XML element instead keeping it in raw form "for later on".</p> <p>Essentially I would like to be able to use this as a Objects representation of the XML.</p> <pre><code>[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; } } </code></pre> <p>where the SalesPerson property on the Cars object would contain a stream with the raw xml that is within the &lt;SalesPerson&gt; xml element after being run through an XmlSerializer.</p> <p>Can this be done? Can I choose to only deserialize "part of" an xml document?</p> <p>Thanks! -Mike</p> <p>p.s. example xml stolen from <a href="https://stackoverflow.com/questions/364253/how-to-deserialize-xml-document">How to Deserialize XML document</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload