Get InnerText from Collection - c#

Is there a way to get the innertext of a node when the node is inside a collection
Currently i have this
Collection<string> DependentNodes = new Collection<string>();
foreach (XmlNode node in nodes)
{
for (int i = 0; i < node.ChildNodes.Count; i++)
{
DependentNodes.Add(node.ChildNodes[i].InnerXml);
//the reason i'm using InnerXml is that it will return all the child node of testfixture in one single line,then we can find the category & check if there's dependson
}
}
string selectedtestcase = "abc_somewords";
foreach (string s in DependentNodes)
{
if(s.Contains(selectedtestcase))
{
MessageBox.Show("aaa");
}
}
When i debug string s or the index has this inside of it[in a single line]
<testfixture name="1" description="a">
<categories>
<category>abc_somewords</category>
</categories>
<test name="a" description="a">
<dependencies>
<dependson typename="dependsonthis" />
</dependencies>
</test>
</testfixture>
What i'm trying to do is when we reach "testfixture 1" it will find "abc_somewords" & search the "dependson typename"node(if any) and get the "typename"(which is "dependonthis").

Could you use linq to xml. Something like the below might be a decent start
xml.Elements("categories").Where(x => x.Element("category").Value.Contains(selectedtestcase));
This is off the top of my head so might will need refining
P.S. Use XElement.Load or XElement.Parse to get your xml into XElements

Since you already working with XmlNode you could use a XPath expression to select the desired textfixture node, and select the dependency value:
XmlDocument doc = // ...
XmlNode node = doc.SelectSingleNode("//testfixture[contains(categories/category, \"abc\")]/test/dependencies/dependson/");
if (node != null)
{
MessageBox.Show(node.Attributes["typename"]);
}
This selects the dependson node which belongs to a testfixture node with a category containing "abc". node.Attributes["typename"] will return the value of the typename attribute.
Edited:
Updated XPath expression to the more specific question information

Assumptions
As you are looping in your code and wanting to create a collection I'm assuming the actual Xml File has several testfixture nodes inside such as the below assumed example:
<root>
<testfixture name="1" description="a">
<categories>
<category>abc_somewords</category>
</categories>
<test name="a" description="a">
<dependencies>
<dependson typename="dependsonthis" />
</dependencies>
</test>
</testfixture>
<testfixture name="2" description="a">
<categories>
<category>another_value</category>
</categories>
<test name="b" description="a">
<dependencies>
<dependson typename="secondentry" />
</dependencies>
</test>
</testfixture>
<testfixture name="3" description="a">
<categories>
<category>abc_somewords</category>
</categories>
<test name="c" description="a">
<dependencies>
<dependson typename="thirdentry" />
</dependencies>
</test>
</testfixture>
</root>
The Code using Linq to Xml
To use Linq you must reference the following name spaces:
using System.Linq;
using System.Xml.Linq;
Using Linq To Xml on the above assumed xml file structure would look like this:
// To Load Xml Content from File.
XDocument doc1 = XDocument.Load(#"C:\MyXml.xml");
Collection<string> DependentNodes = new Collection<string>();
var results =
doc1.Root.Elements("testfixture")
.Where(x => x.Element("categories").Element("category").Value.Contains("abc_somewords"))
.Elements("test").Elements("dependencies").Elements("dependson").Attributes("typename").ToArray();
foreach (XAttribute attribute in results)
{
DependentNodes.Add(attribute.Value.Trim());
}
Result
The resulting Collection will contain the following:
As you can see, only the text of the typename attribute has been extracted where the dependson nodes where in a testfixture node which contained a category node with the value of abc_somewords.
Additional Notes
If you read the xml from a string you can also use this:
// To Load Xml Content from a string.
XDocument doc = XDocument.Parse(myXml);
If your complete Xml structure is different, feel free to post it and I change the code to match.
Have Fun.

I don't know what is "nodes" you are using.
Here is code with your requirement(What I understood).
Collection<XmlNode> DependentNodes = new Collection<XmlNode>();
XmlDocument xDoc = new XmlDocument();
xDoc.Load(#"Path_Of_Your_xml");
foreach (XmlNode node in xDoc.SelectNodes("testfixture")) // Here I am accessing only root node. Give Xpath if ur requrement is changed
{
for (int i = 0; i < node.ChildNodes.Count; i++)
{
DependentNodes.Add(node.ChildNodes[i]);
}
}
string selectedtestcase = "abc_somewords";
foreach (var s in DependentNodes)
{
if (s.InnerText.Contains(selectedtestcase))
{
Console.Write("aaa");
}
}

using System;
using System.Xml;
namespace ConsoleApplication6
{
class Program
{
private const string XML = "<testfixture name=\"1\" description=\"a\">" +
"<categories>" +
"<category>abc_somewords</category>" +
"</categories>" +
"<test name=\"a\" description=\"a\">" +
"<dependencies>" +
"<dependson typename=\"dependsonthis\" />" +
"</dependencies>" +
"</test>" +
"</testfixture>";
static void Main(string[] args)
{
var document = new XmlDocument();
document.LoadXml(XML);
var testfixture = document.SelectSingleNode("//testfixture[#name = 1]");
var category = testfixture.SelectSingleNode(".//category[contains(text(), 'abc_somewords')]");
if(category != null)
{
var depends = testfixture.SelectSingleNode("//dependson");
Console.Out.WriteLine(depends.Attributes["typename"].Value);
}
Console.ReadKey();
}
}
}
Output: dependsonthis

Related

How to get an xml value depending on node attribute in C#?

Let's say we have the following xml :
<?xml version="1.0" encoding="UTF-16"?>
<OrderExchangeMessage version="9.0" type="AddOrder"
xmlns:xs="xxx"
xmlns:xsi="xxx"
xmlns="xxx">
<Command>
<AddOrderRequest>
<Order>
<OrderID>xxx</OrderID>
<InnerOrder>
<RestorationOrder version="5.0">
<ModelElements>
<ModelElement displayName="red car">
<files>
<file path="pathtofile"/>
</files>
</ModelElement>
<ModelElement displayName="red truck">
<files>
<file path="pathtofile"/>
</files>
</ModelElement>
<ModelElement displayName="green car">
<files>
<file path="pathtofile"/>
</files>
</ModelElement>
</ModelElements>
</RestorationOrder>
</InnerOrder>
</Order>
</AddOrderRequest>
</Command>
</OrderExchangeMessage>
How can I retrieve the value of file path only if the attribute of ModelElement contains "red" ?
So I need to know which file goes for a red car and which file for a red truck.
I also tried to get a parent in order to look for child, but got no luck so far.
XmlNodeList? nodeListItems = xmldoc.SelectNodes("/OrderExchangeMessage[#version='9.0']/" +
"Command/AddOrderRequest/Order/InnerOrder/" +
"RestorationOrder[#version='5.0']/" +
"ModelElements");
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlNodeList modelElements = doc.GetElementsByTagName("ModelElement");
foreach (XmlNode modelElement in modelElements)
{
XmlNode displayName = modelElement.Attributes.GetNamedItem("displayName");
if (displayName != null && displayName.Value.Contains("red"))
{
XmlNode path = modelElement["files"]["file"].Attributes.GetNamedItem("path");
Console.WriteLine(path.Value);
}
}
This should do the trick.
xml is the xml document
This code iterates all ModelElement and Checks displayName for the value of red
If you want to use your SelectNodes code, you'll have to have a NameSpaceManager, and the xpath has to have "xxx:" before each node path.
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmldoc.NameTable);
nsmgr.AddNamespace("xxx", xmldoc.DocumentElement.NamespaceURI);
XmlNodeList? nodeListItems = xmldoc.SelectNodes("/xxx:OrderExchangeMessage[#version='9.0']/xxx:" +
"Command/xxx:AddOrderRequest/xxx:Order/xxx:InnerOrder/xxx:" +
"RestorationOrder[#version='5.0']/xxx:" +
"ModelElements", nsmgr);
Please try the following solution.
It is using LINQ to XML API of the .Net Framework. It is available since 2007.
The input XML has a default namespace. It should be taken care of.
c#
void Main()
{
const string filePath = #"e:\Temp\LockedInside.xml";
XDocument xdoc = XDocument.Load(filePath);
XNamespace ns = xdoc.Root.GetDefaultNamespace();
var ModelElements = xdoc.Descendants(ns + "ModelElement")?
.Where(d => d.Attributes("displayName").FirstOrDefault().Value.StartsWith("red"));
foreach (var ModelElement in ModelElements)
{
Console.WriteLine("displayName='{0}', file_path='{1}'"
, ModelElement.Attribute("displayName").Value
, ModelElement.Element(ns + "files").Element(ns + "file").Attribute("path").Value);
}
}
Output
displayName='red car', file_path='pathtofile'
displayName='red truck', file_path='pathtofile'

How do I read Element in specific XML file in C#?

my file XML:
<document xmlns="http://www.abbyy.com/FineReader_xml/FineReader10-schema-v1.xml" version="1.0" producer="ABBYY FineReader Engine 11" languages="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.abbyy.com/FineReader_xml/FineReader10-schema-v1.xml http://www.abbyy.com/FineReader_xml/FineReader10-schema-v1.xml">
<page width="1006" height="636" resolution="300" originalCoords="1" rotation="RotatedUpsidedown">
<block blockType="Text" blockName="" l="979" t="613" r="1006" b="636"><region><rect l="979" t="613" r="1006" b="636"/></region>
<text>
<par lineSpacing="890">
<line baseline="17" l="985" t="620" r="1006" b="636"><formatting lang="EnglishUnitedStates"><charParams l="985" t="620" r="1006" b="636" suspicious="1">r</charParams></formatting></line></par>
</text>
</block>
<block blockType="Barcode" blockName="" l="242" t="21" r="772" b="116"><region><rect l="242" t="21" r="772" b="116"/></region>
<text>
<par><line baseline="0" l="0" t="0" r="0" b="0"><formatting lang="">049102580225180310</formatting></line></par>
</text>
<barcodeInfo type="INTERLEAVED25"/>
</block>
</page>
</document>
I want extract number 049102580225180310 located in <formatting>..</formatting>
I try this code:
XElement racine = XElement.Load("test_XML.xml");
var query = from xx in racine.Elements(XName.Get("block"))
select new
{
CodeBar= xx.Attribute(XName.Get("formatting")).Value
};
But I haven't nothing
Here's a console program, that gets the 2nd formatting (where lang='') node.
using System;
using System.Xml;
namespace ConsoleApplication1 {
class Program {
static void Main(string[] args) {
XmlDocument xml = new XmlDocument();
xml.Load("c:\\temp\\test.xml");
NameTable nt = new NameTable();
XmlNamespaceManager nsmgr;
nsmgr = new XmlNamespaceManager(nt);
nsmgr.AddNamespace("html", xml.DocumentElement.NamespaceURI);
XmlNode ndFormat = xml.SelectSingleNode("//html:formatting[#lang='']", nsmgr);
if (ndFormat != null) {
Console.WriteLine(ndFormat.InnerText);
}
}
}
}
You have a couple issues here:
Your XML has a default namespace in the root node:
<document xmlns="http://www.abbyy.com/FineReader_xml/FineReader10-schema-v1.xml" version="1.0"
Thus all child elements are in this namespace, and so when querying for elements by their Name the appropriate namespace must be used when constructing an XName for which to search.
The <formatting> nodes are not direct children of the <block> nodes, they are nested within several levels of XML. Also, they are XML elements, not XML attributes.
Similarly the <block> elements are not direct children of the <document> root element, they are nested inside a <page> element.
In such cases XElement.Descendants(name) can be used to find nested elements by name.
Thus your query should be:
var ns = racine.Name.Namespace; // The root default namespace used by all the elements in the XML.
var query = from block in racine.Descendants(ns + "block")
from formatting in block.Descendants(ns + "formatting")
select new
{
CodeBar= (string)formatting,
};
Sample fiddle that outputs the values of the two <formatting> elements:
{ CodeBar = r }
{ CodeBar = 049102580225180310 }

how iterate through child element of multiple parents xml file c#

I have an xml file like this:
<post>
<categories>
<category ref="4527" />
<category ref="4528" />
<category ref="4529" />
<category ref="4530" />
<category ref="4531" />
</categories>
</post>
<post>
<categories>
<category ref="4523" />
<category ref="4524" />
<category ref="4525" />
<category ref="4526" />
<category ref="4527" />
</categories>
</post>
Using C# and .Net 4.5 I want to get the first set of category reference numbers, then process them, then move to the next set of category reference numbers and process them. I am hoping that some one can point me in the right direction. I am not sure how to do this using XPath or with Linq to XML or if those are even the right approach. Thanks in advance.
After some responses to some very smart people I was able to use Selman22's train of thought to help me write some XPath. Here is the solution I came up with:
XmlDocument xdoc = new XmlDocument;
xdoc.Load(savePath);
XmlNode root = xdoc.DocumentElement;
// add the namespace
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xdoc.NameTable);
nsmgr.AddNamespace("bml", "http://www.blogml.com/2006/09/BlogML");
//puts the catagories elements into a list
XmlNodeList blogCatagories = root.SelectNodes("descendant::bml:post/bml:categories", nsmgr);
//loop throught list and place the attribute "ref" into a list and traverse each "ref"
foreach (XmlNode nodeCat in blogCatagories)
{
XmlNodeList catagoryids = nodeCat.SelectNodes("descendant::bml:category/#ref", nsmgr);
foreach (XmlNode nodeID in catagoryids)
{
Console.WriteLine(nodeID.InnerText.ToString());
}
}
First get your categories
var xdDoc = XDocument.Load(path);
var categories = xDoc.Descendants("categories").ToList();
Then loop through your category list
foreach(var cat in categories)
{
var numbers = cat.Elements("category").Select(c => (int)c.Attribute("ref"));
foreach(var number in numbers)
{
// process your numbers
}
}
var xdoc = XDocument.Load(path_to_xml);
var query = from p in xdoc.Root.Descendants("post")
select p.Element("categories")
.Elements("category")
.Select(c => (int)c.Attribute("ref"))
.ToList();
This query will return iterator which will get next sequence of category reference numbers each time you are iterating it.
foreach(List<int> references in query)
{
// process list of references
foreach(int reference in references)
// process reference
}
XPathNavigator xml = new XPathDocument(filename).CreateNavigator();
foreach(XPathNavigator categories in xml.Select("//categories"))
{
foreach(XPathNavigator category in categories.Select("category"))
{
string category_ref = category.GetAttribute("ref", string.Empty);
}
// do processing
}
After some responses to some very smart people I was able to use Selman22's train of thought to help me write some XPath.
XmlDocument xdoc = new XmlDocument;
xdoc.Load(savePath);
XmlNode root = xdoc.DocumentElement;
// add the namespace
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xdoc.NameTable);
nsmgr.AddNamespace("bml", "http://www.blogml.com/2006/09/BlogML");
//puts the catagories elements into a list
XmlNodeList blogCatagories = root.SelectNodes("descendant::bml:post/bml:categories", nsmgr);
//loop throught list and place the attribute "ref" into a list and traverse each "ref"
foreach (XmlNode nodeCat in blogCatagories)
{
XmlNodeList catagoryids = nodeCat.SelectNodes("descendant::bml:category/#ref", nsmgr);
foreach (XmlNode nodeID in catagoryids)
{
Console.WriteLine(nodeID.InnerText.ToString());
}
}
I would use XPathDocument and XPathNavigator, lots of examples on google like this
http://www.codegod.com/XPathDocument-XPathNavigator-XPathNodeIterator-sample-with-C-AID504.aspx

Getting an XElement with a namespace via XPathSelectElements

I have an XML e.g.
<?xml version="1.0" encoding="utf-8"?>
<A1>
<B2>
<C3 id="1">
<D7>
<E5 id="abc" />
</D7>
<D4 id="1">
<E5 id="abc" />
</D4>
<D4 id="2">
<E5 id="abc" />
</D4>
</C3>
</B2>
</A1>
This is may sample code:
var xDoc = XDocument.Load("Test.xml");
string xPath = "//B2/C3/D4";
//or string xPath = "//B2/C3/D4[#id='1']";
var eleList = xDoc.XPathSelectElements(xPath).ToList();
foreach (var xElement in eleList)
{
Console.WriteLine(xElement);
}
It works perfectly, but if I add a namespace to the root node A1, this code doesn't work.
Upon searching for solutions, I found this one, but it uses the Descendants() method to query the XML. From my understanding, this solution would fail if I was searching for <E5> because the same tag exists for <D7>, <D4 id="1"> and <D4 id="2">
My requirement is to search if a node exists at a particular XPath. If there is a way of doing this using Descendants, I'd be delighted to use it. If not, please guide me on how to search using the name space.
My apologies in case this is a duplicate.
To keep using XPath, you can use something link this:
var xDoc = XDocument.Parse(#"<?xml version='1.0' encoding='utf-8'?>
<A1 xmlns='urn:sample'>
<B2>
<C3 id='1'>
<D7><E5 id='abc' /></D7>
<D4 id='1'><E5 id='abc' /></D4>
<D4 id='2'><E5 id='abc' /></D4>
</C3>
</B2>
</A1>");
// Notice this
XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
nsmgr.AddNamespace("sample", "urn:sample");
string xPath = "//sample:B2/sample:C3/sample:D4";
var eleList = xDoc.XPathSelectElements(xPath, nsmgr).ToList();
foreach (var xElement in eleList)
{
Console.WriteLine(xElement);
}
but it uses the Descendants() method to query the XML. From my understanding, this solution would fail if I was searching for because the same tag exists for , and
I'm pretty sure you're not quite understanding how that works. From the MSDN documentation:
Returns a filtered collection of the descendant elements for this document or element, in document order. Only elements that have a matching XName are included in the collection.
So in your case, just do this:
xDoc.RootNode
.Descendants("E5")
.Where(n => n.Parent.Name.LocalName == "B4");
Try this
var xDoc = XDocument.Parse("<A1><B2><C3 id=\"1\"><D7><E5 id=\"abc\" /></D7><D4 id=\"1\"><E5 id=\"abc\" /></D4><D4 id=\"2\"><E5 id=\"abc\" /></D4></C3></B2></A1>");
foreach (XElement item in xDoc.Element("A1").Elements("B2").Elements("C3").Elements("D4"))
{
Console.WriteLine(item.Element("E5").Value);//to get the value of E5
Console.WriteLine(item.Element("E5").Attribute("id").Value);//to get the value of attribute
}

reading node from xml file in XMLDocument

i am trying to grab the TopicName how should i go after it and try different combination but somehow i am unable to get TopicName below is my source codee...
XmlDocument xdoc = new XmlDocument();//xml doc used for xml parsing
xdoc.Load(
"http://latestpackagingnews.blogspot.com/feeds/posts/default"
);//loading XML in xml doc
XmlNodeList xNodelst = xdoc.DocumentElement.SelectNodes("content");//reading node so that we can traverse thorugh the XML
foreach (XmlNode xNode in xNodelst)//traversing XML
{
//litFeed.Text += "read";
}
sample xml file
<content type="application/xml">
<CatalogItems xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="sitename.xsd">
<CatalogSource Acronym="ABC" OrganizationName="ABC Corporation" />
<CatalogItem Id="3212" CatalogUrl="urlname">
<ContentItem xmlns:content="sitename.xsd" TargetUrl="url">
<content:SelectionSpec ClassList="" ElementList="" />
<content:Language Value="eng" Scheme="ISO 639-2" />
<content:Source Acronym="ABC" OrganizationName="ABC Corporation" />
<content:Topics Scheme="ABC">
<content:Topic TopicName="Marketing" />
<content:Topic TopiccName="Coverage" />
</content:Topics>
</ContentItem>
</CatalogItem>
</CatalogItems>
</content>
The Topic nodes in your XML are using the content namespace - you need to declare and use the XML namespace in your code, then you can use SelectNodes() to grab the nodes of interest - this worked for me:
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xdoc.NameTable);
nsmgr.AddNamespace("content", "sitename.xsd");
var topicNodes = xdoc.SelectNodes("//content:Topic", nsmgr);
foreach (XmlNode node in topicNodes)
{
string topic = node.Attributes["TopicName"].Value;
}
Just as a comparison see how easy this would be with Linq to XML:
XDocument xdoc = XDocument.Load("test.xml");
XNamespace ns = "sitename.xsd";
string topic = xdoc.Descendants(ns + "Topic")
.Select(x => (string)x.Attribute("TopicName"))
.FirstOrDefault();
To get all topics you can replace the last statement with:
var topics = xdoc.Descendants(ns + "Topic")
.Select(x => (string)x.Attribute("TopicName"))
.ToList();
If you just need a specific element, then I'd use XPath:
This is a guide to use XPath in C#:
http://www.codeproject.com/KB/XML/usingXPathNavigator.aspx
And this is the query that will get you a collection of your Topics:
//content/CatalogItems/CatalogItem/ContentItem/content:Topics/content:Topic
You could tweak this query depending on what it is you're trying to accomplish, grabbing just a specific TopicName value:
//content/CatalogItems/CatalogItem/ContentItem/content:Topics/content:Topic/#TopicName
XPath is pretty easy to learn. I've done stuff like this pretty quickly with no prior knowledge.
You can paste you XML and xpath query here to test your queries:
http://www.bit-101.com/xpath/
The following quick and dirty LINQ to XML code obtains your TopicNames and prints them on the console.
XDocument lDoc = XDocument.Load(lXmlDocUri);
foreach (var lElement in lDoc.Element("content").Element(XName.Get("CatalogItems", "sitename.xsd")).Elements(XName.Get("CatalogItem", "sitename.xsd")))
{
foreach (var lContentTopic in lElement.Element(XName.Get("ContentItem", "sitename.xsd")).Element(XName.Get("Topics", "sitename.xsd")).Elements(XName.Get("Topic", "sitename.xsd")))
{
string lTitle = lContentTopic.Attribute("TopicName").Value;
Console.WriteLine(lTitle);
}
}
It'd have been a lot shorter if it wasn't for all the namespaces :) (Instead of "XName.Get" you would just use the name of the element).

Categories