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
Related
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.
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!
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.
I have an app which should read the data from an xml file and then use that data.
How can I import an xml file in my app (what's the code for that) and how can I use the data from that xml file?
Here's an example of the xml database I use:
<Data>
<Animals>
<A>
<word>Ant</word>
<word>Aardwark</word>
</A>
<B>
<word>Bear</word>
<word>Boa</word>
</B>
</Animals>
</Data>
Also I tried this
XDocument loadedData = XDocument.Load("Data.xml");
to read the data from the xml file but didn't work.
Also the in what form can I use the xml data? In other words the xml data would be in a string format or an "X-Something" format?
Update: Maybe Xml Deserialization would work for me?
Thank you in advance
If "Data.xml" is in the root of the project, make sure the Build Action is set to Content and your code should work.
Linq2XML is your friend, and will help you do just that! Mind you that it'll be read-only, unless you place it in the Isolated Storage.
No need for IsoStore if you already have the file and it is the same for every app instance (given that you only need to read it). Simply do what Matt said to quickly get the contents. I would recommend deserializing it to a separate class, so that you can easily reuse and modify the data.
Now, if you want to store the data, you can later easily serialize the existing class and store it locally. In case you want to go a bit deeper into data storage, you could use SQL CE, that is included in Mango and will allow you to manipulate SDF files (which, by the way, can be loaded separately with app instances). Also, a good idea would be to look into Sterling DB (will use IsoStore).
Using the System.XML namespace, use the following code.
XmlDocument xml = new XmlDocument();
xml.LoadXml("your string of xml");
XmlNode xNode = xml.SelectSingleNode("xpath to a single node");
XmlNodeList xNodeList = xml.SelectNodes("xpath to multiple nodes");
You can treat xNode and xNodeList kind of like array results sets and view their contents using the bracket syntax like xNodeList[0].
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.