xml serialization and comments - c#

I'm working on some custom xml editor. I have a library that contains all the classes that can be de/serialized. The problem is that during deserialization all the comments that were in the xml are lost. Let's say I have a xml structure:
<mainNode>
<subNode name="firstSubNode">
<item prop1="a" prop2="b"/>
<item prop1="c" prop2="e"/>
<!--Here I have important comment-->
<item prop1="d" prop2="f"/>
</subNode>
<!--and also here I have a comment-->
<subNode name="secondSubNode">
<item prop1="a" prop2="b"/>
<item prop1="c" prop2="e"/>
</subNode>
</mainNode>
Now when I deserialize such xml file I will have a main object (mainNode) that contains two subNodes with items. Now if I serialize this object, the result will be the same file but without comments.
The question: is there a way to keep this comments when running serialize/deserialize method? If yes how will this work when I add/remove/edit some nodes/items that contain comments.

Related

C# API for XML structure comparison

is there a way to programmatically compare structures of 2 XML files, but not their values?
More concretely, if you have 2 xml files:
<car>
<numberofwheels>4</numberofwheels>
<carcolor color="red" dateofpainting="2015-10-10" />
</car>
and
<car>
<numberofwheels>7</numberofwheels>
<carcolor color="blue" />
</car>
it would only notice that attribute dateofpainting is missing, but not the change of values (numberofwheels and color). I also don't care about blanks, newlines, attribute order, etc...
There is an XML Diff and Patch Tool from Microsoft, but as far as I can see, it also checks xml values and you cannot set it up to ignore them.
If any one of the structure is predefined,then you can use the XML schema do find the mismatch. If not then you have to traverse the document node by node using XMLDocument/XMLReader class and you can get the difference list.

XML Parsing - Removing nodes and keeping the overall structure

so I have a massive XML file that contains a structure similar to this one:
<Item ItemId=";ResTVersion" ItemType="0" PsrId="245" Leaf="false">
<Disp Icon="Str" Expand="true" Disp="true" LocTbl="false" Order="13352" />
<Modified By="sachink" DateTime="2008-12-16T19:02:35Z" />
<PsrProps>
<Str Name="Kii" Val="yyyyyyyyyyyyy" />
</PsrProps>
<Item ItemId=";ResTFileVersion" ItemType="0;ResT" PsrId="245" InstFlg="true" Leaf="true">
<Str Cat="Text" UsrLk="true">
<Val><![CDATA[ttttttttt]]></Val>
<Tgt Cat="Text" Orig="New">
<Val><![CDATA[ttttttttt]]></Val>
</Tgt>
</Str>
<Disp Icon="Str" Order="13353" />
<Modified By="sachink" DateTime="2008-12-16T19:02:35Z" />
<Cmts>
<Cmt Name="Dev"><![CDATA[{Locked}]]></Cmt>
</Cmts>
</Item>
<Item ItemId=";ResTLanguageTag" ItemType="0;ResT" PsrId="245" InstFlg="true" Leaf="true">
<Str Cat="Text" UsrLk="true">
<Val><![CDATA[en-US]]></Val>
<Tgt Cat="Text" Orig="New">
<Val><![CDATA[en-US]]></Val>
</Tgt>
</Str>
<Disp Icon="Str" Order="13354" />
<Modified By="sachink" DateTime="2008-12-16T19:02:35Z" />
<Cmts>
<Cmt Name="Dev"><![CDATA[=.ABVUDHUIDSHFUIDSHFUISHDFUIDSH iusdhfUIHAs]]></Cmt>
</Cmts>
</Item>
</Item>
I have several item ids and I want to create a new xml that respects the old structure.
I use this code to retrieve the nodes that I want, and then create the new XML.
XmlNodeList nodes = originalXML.SelectNodes("//*[contains(#ItemId,'" + id + "')]");
So what I want is to remove some nodes but I only have the ids of the ones I want to keep.
The problem is how do you keep the outer structure of an xml, when you use the selectnodes function, to get the inner nodes?
Thanks!
I would go the opposite way: remove what you don't need. It's hard to build an XmlDocument from scratch (takes a lot of coding).
I think you will be better off removing the nodes you don't want from the structure you already have.
XmlNodeList nodes = originalXML.SelectNodes("/*[not(contains(#test,'test'))]")
foreach(XmlNode node in nodes)
originalXML.RemoveChild(node);
should work.
If you need to preserve your original structure you can originalXML.Clone() it.
Sidenote: You might want to look into System.Xml.Linq.XDocument and System.Xml.Linq.XElement I find those a lot easier to use.

C# XML - Collapse XML properties into a list/array

I'm trying to serialize an XML file returned from a SOAP API service. The issue is that they have defined an array of objects as such:
<soap_xml>
...
<item __array="true">
<some_property>text here<some_property/>
</item>
<item>
<some_property>text here<some_property/>
</item>
...
</soap_xml>
Is there anyway to use the XmlSerializer to condense this down into an array when deserializing this XML file, or will I have to process the entire XML file manually. I'm not keen on having to process the XML manually, since it has over 100 different properties/fields, but if there is no other solution, then I'll have to use a XMLReader and write a custom serializer.
Also, asking the API provider to change the format of the returned XML is out of the question.
You'll need to define a class that follows the schema of the XML file - do you have a schema from the API provider? If so, you could use xsd.exe to generate a class file that can load it.
Alternatively, you'll need to annotate your own class with the appropriate XML attributes that follows the format.

DataContract/DataMember multiple elements in xml

I have an XML like this:
<data>
<foo>some value</foo>
<result>...</result>
<result>...</result>
<result>...</result>
...
</data>
I would like to deserialize it with DataContract/DataMember..
I know how to handle the array/collection of results elements if they were embedded inside a parent object like:
<data>
<foo>some value</foo>
<collectionOfResults>
<result>...</result>
<result>...</result>
<result>...</result>
...
</collectionOfResults>
</data>
But I don't know how to do it without the embedding element. Do you?
If you need to control the format of the XML, then you don't want to use the DataContractSerializer. Use the XML Serializer instead.

What's the quickest (code execution) way to execute an XML reading?

I have to read the XML:
<items>
<item>
<prop1>value1</prop1>
<prop2>value2</prop2>
<prop3>value3</prop3>
</item>
<item>
<prop1>value1</prop1>
<prop2>value2</prop2>
<prop3>value3</prop3>
</item>
</items>
And put the values into a List<CLASS>.
Some options:
Use XMLSerializer to deserialize to a List
Use XMLDocument to read each item using SelectNodes with XPath and put the values into a List
Use XMLReader to read each node and put the values into a List
Other option...
By far the fastest that I have seen is to use XSD.exe to create an XSD and Class to go with it, then use serialization.
Another option would be to use LinqToXml.
If you're in dotnet, install the WCF starter pack. Then you'll have an option "Paste XML as Types", so you can cut the XML you're looking to serialize into the clipboard and paste it into code as a serializable type. Then you can just serialize the XML and get the values through the class.

Categories

Resources