Read child elements using C# from xml - c#

Greeting,
What is the best practice to read all attributes from a child elements by ID attributes using C# in xml file listed down.
Thank you,
<?xml version="1.0" encoding="utf-8"?>
<WinDLN>
<Program ID="1" Name="CIS562" StartDate="9/8/2010 5:50:00 PM" EndDate="9/8/2010 9:15:00 PM" />
<Program ID="2" Name="CIS532" StartDate="10/8/2010 5:50:00 PM" EndDate="10/8/2010 9:15:00 PM" />
<Program ID="3" Name="ECE552" StartDate="6/8/2010 5:50:00 PM" EndDate="6/8/2010 9:15:00 PM" />
</WinDLN>

The following LINQ call should do the trick:
var attrs =
doc.Descendants("Program").First(prog =>
prog.Attribute("ID").Value == "2").Attributes();
The Descendants method gives you all elements (anywhere) in the XML document that are named "Program". Using First, you can get the first one that matches some specified predicate (e.g. has "ID" equal to "2"). Note that you can use FirstOrDefault if you want to get null when there is no such element. Finally, Attributes gives you a collection of all attribtues of the element.
I think that using LINQ to XML if you can is preferrable - you'll write the same code when working with XML or other data sources, so reading and writing the code is easy (once you learn LINQ).

There are many ways to do it, e.g. LINQ over XML. But using Xpath is definitely not dead yet:
class Program
{
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
string xml = #"... your xml ";
doc.LoadXml(xml);
// Using SelectNodes with Xpath
XmlNodeList list = doc.SelectNodes("WinDLN/Program[#ID='2']");
Console.WriteLine(list.Count); // prints 1
list = doc.SelectNodes("WinDLN/Program[#ID]");
Console.WriteLine(list.Count); // prints 3 (selected all IDs)
}
}
What method you'll choose is most often a matter of taste, select the API you're most comfortable with.

Related

Inserting XML nodes and inner nodes to an existing XML document in C#

Currently I have a working C# program that works as follows:
Accept .xls template with values (xls is manually created by user)
Save the values (matching fields) to the database
Convert and write .xls to XML. Please see below sample output:
Existing XML Structure
Now, what I want to do is:
Read the existing xml (the created xml)
Insert another set of nodes and subnodes (ReleaseLine and sub nodes). It must accept multiple ReleaseLine.
Save/create the new xml with appended nodes. Please see below output:
This is what I'm looking for:
My existing C# program is simple but the XML nodes and hierarchy is bloody deep. I just created the C# code using new XElement method and passing values for each nodes. Then I simply use xmlDocument.Save() method to write the xml.
[Existing XML Program][3]
To add nodes or append content in existing xml-data I´d use Linq to XML.
XElement xml = XElement.Load("file.xml");
xml.Add( new XElement("uberNode",
new XElement("childNode", content),
new XElement("anotherChildNode", content)));
xml.Save("file.xml");
Here are some other related solutions.
Add to specific node (with example):
Following exisiting XML-data:
`<Names>
<Name>
<prename>John</prename>
<lastname>Snow</lastname>
</Name>
<Name>
<prename>Harry</prename>
<lastname>Harry</lastname>
</Name>
</Names>`
Now I want to add an "age"-tag before the first "prename"-tag and a "family"-tag after the first "lastname"-tag.
XElement xml = XElement.Load("file.xml");
var childrens = xml.DescendantsAndSelf().ToArray();
var first_prename = childrens[2];
var first_lastname = childrens[3];
Console.WriteLine(childrens[0]); //prints out the whole content
first_prename.AddBeforeSelf(new XElement("age", 22));
first_lastname.AddAfterSelf(new XElement("family", new XElement("mother", "paula"), new XElement("father", "paul")));
xml.Save("file.xml");
Outcome:
`<Names>
<Name>
<age>22</age>
<prename>John</prename>
<lastname>Snow</lastname>
<family>
<mother>paula</mother>
<father>paul</father>
</family>
</Name>
<Name>
<prename>Harry</prename>
<lastname>Harry</lastname>
</Name>
</Names>`
I was facing the problem and Linq gave me the easiest way to accomplish that!
There are also other similar way e.g. here. But I tried a bit more and DescendantsAndSelf() made it easier for me to go through.
I found an answer to my question, here is the link http://www.xmlplease.com/add-xml-linq
Using XPathSelectElement method, I was able to find the right node and appended new block of XElement.

Extract data from a XML string using linq vs xmlDocument

I have done the below many times using a xmlDocument approach, but I wanted to use the more powerful linq to xml approach.
However, I seem to have run into a wall.
I am getting data back from a restful API from twillio / crmText.
Here is a link to their site where their docs live:
http://crmtext.com/api/docs
Here is my XML string:
<response op="getcustomerinfo" status="200" message="ok" version="1.0">
<customer>
<totalMsg>3</totalMsg>
<custId>9008281</custId>
<custName></custName>
<timestamp>2015-04-30 16:17:19</timestamp>
<optinStatus>3</optinStatus>
<custMobile>6185551212</custMobile>
<subacct>1st Choice Courier</subacct>
</customer>
</response>
I need to find out the optinStatus. It should return 3.
I am using the below, which return the above xml
XDocument xdoc = XDocument.Parse(result1);
I have tried about 4000 different things, including:
IEnumerable<XElement> otinStatus = from el in xdoc.Elements("customer") select el;
IEnumerable<XElement> otinStatus2 = from el in xdoc.Elements("cusotmer.optinStatus") select el;
IEnumerable<XElement> otinStatus3 = from el in xdoc.Elements("optinStatus") select el;
All of which returns no results.
Please help, I know this is something simple I am missing.
Thank you in advance -- Joe
Retrieve an Element's Value
var status = xDoc
.Descendants("optinStatus") // get the optinStatus element
.Single() // we're expecting a single result
.Value; // get the XElement's value
Example
Here is a working Fiddle for you. You can see it running live here. The output is 3.
using System;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
public class Program
{
public static void Main()
{
var xDoc = XDocument.Parse(xmlString);
var status = xDoc.Descendants("optinStatus").Single();
Console.WriteLine(status.Value);
}
private static string xmlString = #"
<response op=""getcustomerinfo"" status=""200"" message=""ok"" version=""1.0"">
<customer>
<totalMsg>3</totalMsg>
<custId>9008281</custId>
<custName></custName>
<timestamp>2015-04-30 16:17:19</timestamp>
<optinStatus>3</optinStatus>
<custMobile>6185312349</custMobile>
<subacct>1st Choice Courier</subacct>
</customer>
</response>
";
}
Explanation
Descendents() is an instance axes method (or just axes in shorthand). It returns an IEnumerable<XElement> of all matching descendents. On its results, we call Single(). It is a Linq method that returns the only element of a sequence. If there is more than one element, it throws an error. We're left with a single XElement. This represent an entire XML element. Since we only want its value not the entire element, we call the Value property. Bingo, we're done.
A Bit More Detail
Axes come in two kinds:
Instance axes methods, which MSDN lists here, are invokable members of the XElement, XDocument, and XNode classes.
Extension axes methods, which MSDN lists here, are invokable on collections.
With one exception, an axes method returns a collection of type IEnumerable<T>. The exception is Element(), which returns the first matching child object. That what AmatuerDev used and, as in your question, if you are only expecting a single result, it is a just as good if not better approach that is Descendants().
Retrieve an Attribute Value
Once we have an XElement, we can retrieve one of its attributes instead of its value. We do that by calling the Attributes() method. It returns the matching XAttribute. Since we only want the attribute value, we call the Value property. Voila.
// for attribute
var response = xDoc.Descendants("response").Single();
var attr = response.Attribute("status");
General Approach
Using Linq to XML is a two step process.
Call an axes method to obtain an IEnumerable<T> result.
Use Linq to query that.
See Also
Here is some relevant MSDN documentation:
How to: Retrieve the Value of an Element (LINQ to XML)
LINQ to XML Axes
Assuming xDoc being the XDocument. Have you tried..
var customer = xDoc.Root.Element("customer");
var optinStatus = customer.Element("optinStatus");
var optinStatusValue = optinStatus.Value;

combine two xml_docs C# xmlDocument

I have been using the following SO, How to merge two XmlDocuments in C#, to try and merge two sample xml files into one. Essentially here are my two xml files
test1.xml:
<root>
<Name>
<id>4</id>
<First_name>Tony</First_name>
<last_name>""</last_name>
</Name>
<Name>
<id>6</id>
<First_name>Donny</First_name>
<last_name>Test</last_name>
<middle_name>Ward</middle_name>
</Name>
</root>
And test2.xml
<root>
<Name>
<id>1</id>
<First_name>Nick</First_name>
<last_name>Tester</last_name>
</Name>
<Name>
<id>2</id>
<First_name>Lauren</First_name>
<last_name>MsTester</last_name>
</Name>
</root>
I am using this code snippet it to merge the into the one file but the procedure is not actually combining them. I am a little knew to xmlDocument class and still trying to navigate this issue correctly.
Code:
XmlDocument xmlreader1 = new XmlDocument();
xmlreader1.Load("C:\\Temp\\test1.xml");
XmlDocument xmlreader2 = new XmlDocument();
xmlreader2.Load("C:\\Temp\\test2.xml");
foreach (XmlNode node in xmlreader2.DocumentElement.ChildNodes)
{
XmlNode importedDocument = xmlreader1.ImportNode(node, true);
xmlreader1.DocumentElement.AppendChild(importedDocument);
}
I believe this is correct to take each child node of root (in this case <name>) from test2.xml and append it to test1.xml. I have also tried as:
XmlNode importedDocument = xmlreader1.ImportNode(xmlreader2.DocumentElement, true);
xmlreader1.DocumentElement.AppendChild(importedDocument);
to no avail again any suggestions would be greatly appreciated.
It's not entirely clear what merge operations you need to perform - but if you just need to copy the root node's child elements, that's really simple with LINQ to XML:
XDocument doc1 = XDocument.Load("test1.xml");
XDocument doc2 = XDocument.Load("test2.xml");
doc1.Root.Add(doc2.Root.Elements());
doc1.Save("test3.xml");
This will not perform any actual merging though - if you have elements with the same ID in both files, you'll end up with both elements in the result, rather than one merged element.
Personally I always prefer using LINQ to XML over the old XmlDocument API - I would strongly recommend using it when you have the opportunity to do so.

Getting to "bottom" of XmlDocument - C#

I have two versions of XmlDocument
Version 1
<?xml version="1.0" encoding="UTF-8"?>
<topElement>
<childElement1>Value</childElement1>
<childElement2>Value</childElement2>
...
</topElement>
Version 2
<?xml version="1.0" encoding="UTF-8"?>
<topElement>
<group1>
<childElement1>Value</childElement1>
<childElement2>Value</childElement2>
</group1>
<group2>
<childElement1>Value</childElement1>
<childElement2>Value</childElement2>
</group2>
</topElement>
In both occasions I need to get all the values for all childElements and add them to collection of a CustomObject.
As far as I understand this could be done through iteration only.
So I get the top node, and go like this:
CustomObject getLow(XmlNode node, CustomObject customObject)
{
foreach (XmlNode n in node.ChildNodes)
{
if (n.HasChildNodes == true)
{
getLow(n);
}
customObject.collection.Add(n.Name, n.InnerText);
}
return customObject;
}
No doubt it is wrong, please can somebody help me getting the correct result in both cases?
You can use Xpath with your XmlDocument:
XmlDocument xmlDoc = new XmlDocument("yourxml.xml");
foreach (XmlNode childElement in xmlDoc.SelectNodes("//childElement"))
{
customObject.collection.Add(childElement.Name, childElement.InnerText);
}
Looping isn't the only way - another way would be to generate an object graph of custom classes with XML attributes and use the XML serilizer to convert the XML into an object graph.
See http://msdn.microsoft.com/en-gb/library/system.xml.serialization.xmlserializer.aspx for details.
An alternative to that would be to use an XDocument and use linq to query the values you want straight out of the XDocument.
See Using Linq and XDocument, can I get all the child elements under parent tag?
Hope this helps
You should be able to use some xPath to get the nodes you are looking for.
Try something like
node.SelectNodes("//*[count(child::*)=0]")

trouble parsing deeply nested attributes using LINQ to XML

I have been trying to parse this xml in c#
<schema uri=http://blah.com/schema >
<itemGroups>
<itemGroup description="itemGroup1 label="itemGroup1">
<items>
<item description="The best" itemId="1" label="Nutella"/>
<item description="The worst" itemId="2" label="Vegemite"/>
</items>
</itemGroup>
</itemGroups>
</schema>
\itemGroup1\Nutella-The best
\itemGroup1\Vegemite-The worst
Any help or direction would be appreciated.
XDocument xDoc = XDocument.Load(myXml); //load your XML from file or stream
var rows = xDoc.Descendants("item").Select(x => string.Format(
#"\{0}-{1}\{2}-{3}",
x.Ancestors("itemGroup").First().Attribute("description").Value,
x.Ancestors("itemGroup").First().Attribute("label").Value,
x.Attribute("label").Value,
x.Attribute("description").Value));
Let's break down what we're doing:
xDoc.Descendants("item") gets us all <item> elements in the entire document
Select(x => string.Format(format, args) projects each <item> we got from the last operation into whatever format we specify in the lambda. In this case, a formatted string.
In terms of the XML tree, we're "sitting at" the <item> level, so we need to roll back up the tree to get the data for the parent group using Ancestors. Since that method returns a sequence of elements, we know we want the first (nearest to us) so we can read its attribute.
Now you have an IEnumerable<string>, one for each <item> in your XML document and the information in the format you specified:
foreach(string row in rows)
{
Console.WriteLine(row);
}

Categories