I'm trying to select nodes using xpath in c#
This is my XML file
<?xml version="1.0" encoding="utf-8"?>
<xObject version="3.0.2002.0" xmlns="http://schemas.microsoft.com/wix/2006/objects">
<section id="*" type="product">
<table name="NotThis">
<row sourceLineNumber="D:\bla\bla\">
<field>Borderish.fo</field>
<field>Documents</field>
<field>1</field>
<field>No, not this line here 1</field>
</row>
<row sourceLineNumber="D:\blah\blah\">
<field>Charterish</field>
<field>Documents</field>
<field>1</field>
<field>No not, this line here 2</field>
</row>
</table>
<table name="XFile">
<row sourceLineNumber="D:\bla\bla\">
<field>Borderish.fo</field>
<field>Documents</field>
<field>1</field>
<field>This line here 1</field>
</row>
<row sourceLineNumber="D:\blah\blah\">
<field>Charterish</field>
<field>Documents</field>
<field>1</field>
<field>This line here 2</field>
</row>
</table>
</section>
</xObject>
This is my C# code which seems to not work
XmlDocument doc = new XmlDocument();
doc.Load("Testing.xml");
XmlNode root = doc.DocumentElement;
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("ns", "xmlns="http://schemas.microsoft.com/wix/2006/objects"");
XmlNodeList nodeList = root.SelectNodes("ns:table[#type='XFile']/ns:row", nsmgr);
foreach (XmlNode xn in nodeList)
{
string fieldLine = xn["Field"].InnerText;
Console.WriteLine("Field: {4}", fieldLine);
}
What I want to output is every 4th field table name="xfile", like this:
This line here 1
This line here 2
Please let me know if you know a solution or a better way.
First - you should provide just uri for namespace:
nsmgr.AddNamespace("ns", "http://schemas.microsoft.com/wix/2006/objects");
Second - you should use namespace when providing node name. And table has attribute name instead of type:
XmlNodeList nodeList = root.SelectNodes("//ns:table[#name='XFile']/ns:row", nsmgr);
And last - after selecting row nodes, you should select fourth field node (which has full name ns:field):
foreach (XmlNode row in nodeList)
{
XmlNode field = row.SelectSingleNode("(ns:field)[4]", nsmgr);
Console.WriteLine("Field: {0}", field.InnerText);
}
Output:
Field: This line here 1
Field: This line here 2
NOTE: You can get fields directly, without looping on rows:
XmlNodeList fields =
root.SelectNodes("//ns:table[#name='XFile']/ns:row/ns:field[4]", nsmgr);
Related
In XML Document:
Foo.xml
<products>
<product>
<id>1</id>
<name>Foo</name>
</product>
<product>
<id>2</id>
<name>Bar</name>
</product>
</products>
How to get this root element, iterate over his child elements and get their properties?
Bar.cs
XmlDocument doc = new XmlDocument();
doc.Load(path + "/foo.xml");
XmlNode mainNode = doc.DocumentElement.SelectSingleNode("products");
XmlNode root = mainNode.FirstChild; //null
foreach (XmlNode node in mainNode)
{
int id = Convert.ToInt32(node["id"].InnerText);
string name = node["name"].InnerText);
list.Items.Add(id);
list.Items.Add(name);
}
This code implicates that mainNode is null. What is the best practise of doing that?
The DocumentElement is the outermost element of the XML, i.e. the <products> element. You can't select another <products> element below it.
What you can do:
XmlNode mainNode = doc.SelectSingleNode("products");
or
XmlNode mainNode = doc.DocumentElement;
or
XmlNode mainNode = doc.DocumentElement.SelectSingleNode("//products");
The second one is probably the fastest, since it does not need to parse and process a query. The last one is overkill and should be avoided for clean code reasons (KISS principle).
I have this xml file:
<table head="Film">
<row>
<id>USD</id><jan>Jan</jan><feb>Feb</feb><mar>Mar</mar><apr>Apr</apr><maj>May</maj><jun>Jun</jun><jul>Jul</jul><aug>Aug</aug><sep>Sep</sep><okt>Oct</okt><nov>Nov</nov><dec>Dec</dec><sum>Year</sum>
</row>
<row>
<id>2018</id><jan>7629</jan><feb>6433</feb><mar>5573</mar><apr>3676</apr><maj>2545</maj><jun>2542</jun><jul>266</jul><aug>276</aug><sep>2690</sep><okt>371</okt><nov>5446</nov><dec>754</dec><sum>52731</sum>
</row>
I'm trying to extract the individual values for every month.
I've tried
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("model.xml");
XmlNodeList nodeList = xmlDoc.GetElementsByTagName("table");
foreach (XmlNode node in nodeList) // for each <testcase> node
{
Console.WriteLine(node["row"].InnerText);
}
This gives an exception because node["row"] is empty.
Any ideas?
Firstly your XML is not valid. You need to have a </table> on there.
//In this example GetXml() just returns your XML
var doc = XDocument.Parse(GetXml());
var rows = doc.Descendants("table").Elements("row").ToList();
foreach(var element in rows[1].Elements()){
Console.WriteLine(element?.Value);
}
Now this is just a basic example based of your XML. You would likely want it to be more robust. You will notice I am showing you this with LINQ, I feel it's more readable than XmlDocument.
You need to loop through the child nodes to get your desired result as follow:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("model.xml");
XmlNodeList nodeList = xmlDoc.GetElementsByTagName("table");
foreach (XmlNode node in nodeList) // for each <testcase> node
{
foreach (XmlNode row in node.ChildNodes)
{
foreach (XmlNode mon in row.ChildNodes)
{
}
}
}
I have an XMLDocument as result of query.
I would like to extract the <Property> value and the appropriate <Notes> for each entry.
<?xml version="1.0"?>
<EADATA version="1.0" exporter="Enterprise Architect">
<Dataset_0>
<Data>
<Row>
<PropertyID>439</PropertyID>
<Object_ID>683</Object_ID>
<Property>tagged value</Property>
<ea_guid>{5BF3E019-277B-45c2-B2DE-1887A90C6944}</ea_guid>
</Row>
<Row>
<PropertyID>444</PropertyID>
<Object_ID>683</Object_ID>
<Property>Another Tagged value</Property>
<Notes>Another tagged value notes.</Notes>
<ea_guid>{42BE8BAA-06B8-4822-B79A-59F653C44453}</ea_guid>
</Row>
</Data>
</Dataset_0>
</EADATA>
However, If the <Notes> is empty there is no <Notes> tag at all.
What XPath should I write in such cases?
Which value do you want if there is no Notes element, a null, an empty string?
I would select the Row elements with SelectNodes and then check whether a Notes child exists and assign null (as done below) or the empty string if not:
foreach (XmlElement row in doc.SelectNodes("//Row"))
{
string prop = row.SelectSingleNode("Property").InnerText;
string notes = row.SelectSingleNode("Notes") != null ? row.SelectSingleNode("Notes").InnerText : null;
}
Try this :
XPathDocument docNav = new XPathDocument(new StringReader(xml));
XPathNavigator navigator = docNav.CreateNavigator();
XPathNodeIterator NodeIter = navigator.Select("/EADATA/Dataset_0/Data/Row");
foreach (XPathNavigator selectedNode in NodeIter)
{
var a= "<root>" + selectedNode.InnerXml + "</root>";
var x= XDocument.Parse(a);
Console.WriteLine (x.Root.Element("Property").Value);
if (x.Root.Element("Notes")!=null)
Console.WriteLine (x.Root.Element("Notes").Value);
}
result :
tagged value
Another Tagged value
Another tagged value notes.
I don't how to extract the values from XML document, and am looking for some help as I'm new to C#
I am using XmlDocument and then XmlNodeList for fetching the particular XML document
Here is my code
XmlNodeList XMLList = doc.SelectNodes("/response/result/doc");
And my XML looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<result>
<doc>
<long name="LoadID">494</long>
<long name="EventID">5557</long>
<str name="XMLData"><TransactionDate>2014-05-28T14:17:31.2186777-06:00</TransactionDate><SubType>tblQM2222Components</SubType><IntegerValue title="ComponentID">11111</IntegerValue></str></doc>
<doc>
<long name="LoadID">774</long>
<long name="EventID">5558</long>
<str name="XMLData"><TransactionDate>2014-05-28T14:17:31.2186777-06:00</TransactionDate><SubType>tblQM2222Components</SubType><IntegerValue title="ComponentID">11111</IntegerValue></str></doc>
</result>
</response>
In this i have to fetch every the XMLData data that is under every doc tag and i have to fetch last doc tag EventID.
var xml = XDocument.Parse(xmlString);
var docs = xml.Root.Elements("doc");
var lastDocEventID = docs.Last()
.Elements("long")
.First(l => (string)l.Attribute("name") == "EventID")
.Value;
Console.WriteLine ("Last doc EventId: " +lastDocEventID);
foreach (var doc in docs)
{
Console.WriteLine (doc.Element("str").Element("TransactionDate").Value);
}
prints:
Last doc EventId: 5558
2014-05-28T14:17:31.2186777-06:00
2014-05-28T14:17:31.2186777-06:00
You can use two XPath expressions to select the nodes you want. To answer each part of your question in turn:
To select all of the XMLData nodes:
XmlNodeList XMLList
= doc.SelectNodes("/response/result/doc/str[#name='XMLData']");
To select the last EventId:
XmlNode lastEventIdNode =
doc.SelectSingleNode("/response/result/doc[position() =
last()]/long[#name='EventID']");
If not all doc nodes are guaranteed to have an event id child node, then you can simply:
XmlNodeList eventIdNodes =
doc.SelectNodes("/response/result/doc[long[#name='EventID']]");
XmlNode lastNode = eventIdNodes[eventIdNodes.Count - 1];
That should give you what you've asked for.
Update;
If you want the XML data inside each strXml element, you can use the InnerXml property:
XmlNodeList xmlList
= doc.SelectNodes("/response/result/doc/str[#name='XMLData']");
foreach(XmlNode xmlStrNode in xmlList)
{
string xmlInner = xmlStrNode.InnerXml;
}
There's one result tag short in your xml.
Try using this. It's cleaner too imho
XmlNodeList docs = doc.SelectSingleNode("response").SelectSingleNode("result").SelectNodes("doc");
Then you can use a combination of SelectSingleNode, InnerText, Value to get the data from each XmlNode in your list.
For example if you want the EventID from the first doc tag:
int eventID = int.Parse(docs[0].SelectSingleNode("long[#name='EventID']").InnerText);
I am trying to add an XML node to multiple parent nodes(which have same name). But it is only adding to the Last node of the XML and not in all.
input XML
<Record>
<Emp>
<ID>12</ID>
<Name>ABC</Name>
</Emp>
<Emp>
<ID>12</ID>
<Name>ABC</Name>
</Emp>
</Record>
I want to add the Location element to every Emp node. My code is as below:
XmlNodeList xNodeList = doc.SelectNodes("/Record/Emp");
XmlElement xNewChild = doc.CreateElement("Location");
xNewChild.InnerText = "USA";
foreach (XmlNode item in xNodeList)
{
item.AppendChild(xNewChild);
}
doc.Save(path);
but I am getting output like this:
<Record>
<Emp>
<ID>12</ID>
<Name>ABC</Name>
</Emp>
<Emp>
<ID>12</ID>
<Name>ABC</Name>
<Location>USA</Location>
</Emp>
</Record>
The Location element has not been added to the first Emp node.
Note: After debugging, I am able to find that the element has been added even for the first Emp node. But, in the saved XML file I am seeing this strange behavior.
Your xNewChild is a single new element. Simply adding it to multiple nodes will only serialize to the last node. A change like this should work:
XmlNodeList xNodeList = doc.SelectNodes("/Record/Emp");
foreach (XmlNode item in xNodeList)
{
XmlElement xNewChild = doc.CreateElement("Location");
xNewChild.InnerText = "USA";
item.AppendChild(xNewChild);
}
doc.Save(path);