Nesting XML and using DataSets etc - c#

I have a nested XML document and I am looking at the plausability of using DataSets to parse it.
<?xml version="1.0" standalone="yes"?>
<Workbench>
<Overrides>
<Override name='firstoverride' value='overridevalue'/>
</Overrides>
<DataSets>
<BASIC>
<MEMBNO>1</MEMBNO>
<PERSONNO>0</PERSONNO>
</BASIC>
</DataSets>
</Workbench>
What i want to be able to do is essentially access the contents of the Overrides and DataSets as if there were an actual Dataset.
So to validate I check the root element is workbench.
Then I check to see if there are any overrides, I then want to be able to iterate around the Override Items.
Following that, and this is the hard part I want to support abitary but well form XML that will be inserted into a database but the parsing code can make so assumptions about the data as I want it to be generic.
I can do this if I make the DataSets the root element and iterate around it but it doesn't seem to work if nested?
hlep!

Related

Fastest Way of reading XML

I have an XML file in which I store data about a list of persons and another one in which I store a list of objects like this.
people.xml
<People>
<Person>
<Name>itsName</Name>
<Age> itsAge </Age>
<RecentAcquisitions>
<Acquisition>
<name>Apple</name>
<quantity>5</quantity>
</Acquisition>
</RecentAcquisitions>
</Person>
</People>
objects.xml
<Objects>
<Object>
<Name>Apple</Name>
<Description>Fresh Apple</Description>
<Price>10</Price>
<etc>..lots of attributes..</etc>
</Object>
</Objects>
What is the most efficient way of extracting information from objects.xml based on the person Acquisition List at the runtime? (in example the person should have 5 objects of type "Apple").
Momentarily I use a solution which consists of storing each object from objects.xml in a list and when I'm loading a person I search for the respective object based on Acquisition->Name and add it in the person.AquisitionList;
Is there another way of doing this?
Maybe I misunderstood the XML role but it feels wrong to store the information from an XML file in a list or array at runtime.
to my knowledge, using the runtime memory instead of constant read-write operations is the best way to do it / what you're doing is the right way.
XML can be seen as 2 things:
1 - A way to store information, much like a database, until it needs to be retrieved for processing at runtime
this is what you are doing now... you store the objects list on disk using XML, and then you retrieve it for processing/load it into memory at runtime.
2 - A standardized way of passing information around, regardless of technology.
XML can be read in a multitude of languages and any language that can read a string can technically read and extract the data from an XML document.

Configurable code logic in C#

I have a xml data where nodes are present like this
<segment>
<country>US</country>
<prop>Supplier</prop>
</segment>
The scenario is my business entity is strongly bound with this XML.
Now we have to rearchitect the system to make it more scalable. The node names in the xml may change in the future.
<prop>Supplier</prop>
may change to
<name>Supplier</name>
So how to write a dynamic C# code to support this feature ?
You can add version or parsing engine to your xml, so it can look something like that:
<root>
<parseEngine type="version2" />
<!-- Rest of xml -->
</root>
And in C# you you first read this node and then select parsing method.
I have finally written code to fetch the data using XML configuration.
<Properties>
<NodeName>prop<NodeName>
</Properties>
So now I will read this configuration and fetch the data from XML using the node name from Configuration.By This way I can dynamically process my data.
Thanks

Serialize only a part of xml file and save it

I have a problem with respect to XML Serialization. I shall try to explain it with the following example xml file
<AutoExpo>
<Details>
<Venue>XYZ</Venue>
<StartTime>09:00</StartTime>
<EndTime>21:00</EndTime>
</Details>
<Cars>
<Car>
<Company>Chevrolet</Company>
<Model>Cruz</Model>
<Color>Red</Color>
</Car>
<Car>
<Company>Ford</Company>
<Model>Fiesta</Model>
<Color>Blue</Color>
</Car>
</Cars>
</AutoExpo>
Now, when I read this xml file, I deserialize the cars into objects. The car list can be huge. My code uses this objects and can change the properties of some cars. Now what if I want to serialize only those car objects whose properties have changed, back to the xml file and save it so that next time when my code starts it gets the latest state information.
It would be quite difficult to jump around in the XML file changing properties here and there, wherever they have changed. You should just read the whole file into memory, and when you save, write out the whole thing, overwriting the old file.
XML isn't a terrible way of doing this, but as far as I can tell from the question, a SQL Server (or other RDBMS) database would be much more appropriate. You won't have to worry about issues like this, as the DB engine will do that for you.
Although it may not be the best solution, a potentially viable option would be to serialize the edited list to a seperate file and, in code, compare the two files. If there hasn't been any changes to the information the two text files should be identical. If not, you can replace the old file with the new file. The easiest way would be, rather than serialize to a file and read/write it, perhaps send it to a stream and compare them.
When you serialize an object, it generates the entire XML document. So, if you save that to a file, it will overwrite the previous content of the file. Therefore, if you want the resulting file to contain all the cars, including, but not limited to, the modified ones, then you need to serialize the whole thing. If you only serialized the ones that changed, the file would lose all the cars that did not change. If you really do only want to serialize the changed cars, I would suggest creating a new instance of the AutoExpo object and only insert into it the cars that you want to save, then serialize that object with only the partial list.
If you need to just modify a single element in the XML without touching the rest of it because the data is too big, XML is not a good choice. I would suggest a relational database instead. Alternatively, you could store each car as its own XML file and only load and save each one individually as necessary.
You cannot do that with XML. Consider using a relational database. Relational databases have a built-in file space management mechanism allowing doing exactly what you need. You can update single records, add and delete records.
A Jet .mdb database (Access) is a good candidate for the replacement of a XML-File. You can access it via OLEDB with the restriction that the application must be compiled for 32 bit. Access needs not to be installed.
First of all, your entities must have unique identifiers.
<AutoExpo>
<Details>
<Venue>XYZ</Venue>
<StartTime>09:00</StartTime>
<EndTime>21:00</EndTime>
</Details>
<Cars>
<Car id="1">
<Company>Chevrolet</Company>
<Model>Cruz</Model>
<Color>Red</Color>
</Car>
<Car id="2">
<Company>Ford</Company>
<Model>Fiesta</Model>
<Color>Blue</Color>
</Car>
</Cars>
</AutoExpo>
Now you could use XPath to select those nodes that require updates and change their content.
load the document into an XDocument
find a car: document.Element("Car[id=2]")
set the new value: element.Element("Color").Value = "Black"
However, the downside of using a file-based storage remains. You still have to load the whole file into memory and write it back to the hard drive when you're down updating, but you do not have to serialize all Car objects.
I can't think of an easy way to stream the file from hard drive and manipulate it in one go.

Save And Load Data From XML Without Use XMLSerialization

I know that there is a way to save data in an xml file with classes and their properties using XML Serialization.But Is there any way to save data without using classes and XML Serialization???
Absolutely - I would recommend LINQ to XML. For example:
XDocument doc = new XDocument(
new XElement("root",
new XElement("child1", "text"),
new XElement("child2",
new XElement("grandchild"))));
doc.Save("test.xml");
Obviously any of these literals can be supplied from your object data - and LINQ to XML makes it easy to create XML from sequences, LINQ queries etc.
Resulting test.xml file:
<?xml version="1.0" encoding="utf-8"?>
<root>
<child1>text</child1>
<child2>
<grandchild />
</child2>
</root>
LINQ to XML is a lovely API - much nicer than the old XmlDocument one, IMO.
Absolutely, You can leverage Linq to Xml to create xml and then persist it to disk. Check out this Getting Started guide. Specifically, take a look the "Creating XML Trees" and "Serializing XML Trees" sections.
Save And Load Data with XmlDocument Class or use Linq to XML
http://support.microsoft.com/kb/301233
There are quite a few different ways to accomplish this.
Construct an XMLDocument and write code to create the document.
Use LINQ to XML.
Use a class derived from XmlWriter and write code to create the document.
Use a manually created DataSet and write code to populate it.
Use a strongly-typed DataSet created with the designer in Visual Studio and write code to populate it.
Depending on your needs you might also consider using the DataSet directly in lieu of classes.

Generate LINQ to XML C# code, from an XML document?

Does anybody know of a tool that will generate LINQ to XML code from a real XML document or fragment? It's reverse-engineering the common scenario of generating XML.
For example, I want to provide an XML fragment as input like this
<root>
<thing>value</thing>
</root>
and have it generate the equivalent C# LINQ to XML code snippet like so
var x = new XElement("root",
new XElement("thing", new XText("value"));
);
Although I'm looking for a quickie, I'm sure some enterprising individuals will tell me to roll my own and provide some awesome reference code.
See this tool.
the application supports :
XDocument
XDeclaration
XProcessingInstruction
XComment
XNamespace
XElement
XAttribute
generation of business objects
generation of code Linq To Xml (with variables, in method, extraction of
the code corresponding to the selected
nodes)
you can open a Xml file or directly copy to stick xml in the richtextbox
the editor allows to create Xml documents from scratch or to
add/modify existing Xml documents
the editor has several views which are synchronized (Text, treeview)
a help with the seizure (auto completion tags and attributes and
checking in the course of the good
formation of xml) for the text view,…
you can also post the data of the nodes selected in a datagridview
etc
This wouldn't be hard to do using T4 templates, or an XSL transform for that matter, but I don't know anyone who's done it.

Categories

Resources