C# program to read, write, query xml - c#

I would like to create a C# program to write, read, query xml file. I am very new to xml using C#. Can anyone please help me..
specifically i want to do this:
<streets>
<street1>
<house1 no=1 color=red/>
</street1>
<street2>
<house2 no=2 color=blue/>
</street2>
</streets>
I want to read this xml file and print all the houses and their properties.
I want to append to this xml file any new houses
I want to query where any specific house is located.
Can anyone please help me in this?

You can use LINQ. Please have a look at this article: http://www.codeproject.com/Articles/24376/LINQ-to-XML. Hope this help!

If you are interested in creating an application to create some data on 'House' and want to have the capabilities of editing/updating, adding and removing such data, and also want to store it in XML, then it is simple. Here I am considering that you are not very interested to know the exact metadata of the XML and the XML need not be following some predefined schema.
For such a requirement I worked sometime back and you may look at this for the same:
http://www.geekays.net/post/2011/03/24/XML-Data-Storage-and-XmlSerializer-The-easy-data-store.aspx

Related

Write into a json data file without rewriting the whole thing?

I'm using a json file as a data source in a small app written in C#. I spent last evening looking after the best ways to do CRUD actions with a json data source and the one thing bugging me is that it doesn't seem possible to add/modify data without serializing the whole file to my class, adding an item to the list/updating an item then rewrite the whole thing to the file.
Something like that, which I'd like to avoid if possible :
var allUsers = jData.ToObject<List<JsonEntry>>();
allUsers.Add(newEntry);
var convertedItems = JsonConvert.SerializeObject(allUsers, Formatting.Indented);
File.WriteAllText("UsersConfiguration.json", convertedItems);
Any idea on how to handle writing/updating in a better way ?
Thanks in advance !
You could engineer something involving json chunks, but it would be hacky. The safest way is to carry on as you are, or maybe consider a NoSQL database since you're up against the "if only this file wasn't really a file" problem :P

How to read an xaml file and get the data I want in C#

I want to read through a xaml file, and find all the lines with 'Annotation.AnnotationText' and get specific data from that line.For example, this line:
<prwab:Branch Condition="{x:Null}" sap2010:Annotation.AnnotationText="testing information " ContinuouslyExecute="False" CreatedBy="System Administrator" CreatedOn="2013-02-23T14:51:28.1555955-05:00" DisplayName="Failure" EnableValidationRule="False" sap:VirtualizedContainerService.HintSize="160,234" ID="ab91dec8-1976-491e-91eb-58e073a69d16" IsReportable="False" LastModifiedBy="System Administrator" LastModifiedOn="2013-02-23T14:51:28.1555955-05:00" MediaRecord="[MediaRecord]" SystemName="CollectDigitsActivity1 Failure6" Timeout="10000" Type="Voice">
I want find all the lines with 'AnnotationText' in my xaml file, and get information like text = 'testing information', id = 'ab91dec8-1976-491e-91eb-58e073a69d16' , created date and lastmodified date.
I have 0 knowledge in this area and I don't know where to start and which method should I use. Thanks for helping!
XAML is just a specific flavour of XML. You will need to use XML parsing to read the file into an object that you can process in this manner. I recommend Linq to XML for this (look at XDocument class to get started), specifically as finding values by XName using a specific namespace as you will need to for the "sap2010" namespace is very easy.
You can then easily parse and extract the information you are looking for using those classes.

How to move through different records in C#

I made a form that basically displays information about different vendors from an XML file that I was given. The XML file is retrieved from a class that I created, called VendorsDB.cs. On my form, I have a Previous and Next button that I want to display the next vendor or the previous vendor (Vendor1, Vendor2...) but I have no idea what method to use. I know I have to use a loop but I'm not sure as to how to code the loop. I've just started programming with C# so I'm really lost. Any help would be greatly appreciated!
Your question is missing alot of information, but what im seeing is that you need to do some work with XML.
I suggest you look into Linq To XML which enabled you to query tags and attributes with query syntax.
A very simple query good look like this:
// Load the xml from the specified path
var xml = XDocument.Load(#"LocationOfXml");
// Query the first element with a "MyXmlTag" as name
var someAttribute = xml.Descendants().FirstOrDefault(x => x.Name == "MyXmlTag");
The query language is far richer than this simple query. Read up and im sure you'll be able to get it working asap.

Reading and using an XML file as a database - Windows Phone 7 app

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].

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