WPF get every 'secondary child nodes' in a master node - c#

I have the current xml file:
<?xml version="1.0"?>
<Master>
<Child1>
<Display>Some things here</Display>
<Link>http://google.ca</Link>
<Description>Desc</Description>
<Image>http://google.ca</Image>
</Child2>
</Master>
I already figured out how to get the link using doc.SelectSingleNode("Master/Child1/Link").InnerText;
But now, I need a way to list every child (Like Child1, there is way more than that which all have subnodes like link, display....)
I tried a bunch of things but all I found online was how to get "name" from <Master name="Name Here"/>
Also, I'd need it to act as a String (being able to print it to console without getting System.xml.XmlNode)
Thanks for your help.

In xpath * matches any node.
var nodes = doc.SelectNodes("Master/*/Link");
foreach (XmlNode node in nodes)
Console.WriteLine(node.InnerText);
var nodes = doc.SelectNodes("Master/*");
foreach (XmlNode node in nodes)
Console.WriteLine(node.Name);

Related

Modify xml with Xmldocument in C#

I am trying to edit the values of an xml document following the instructions found on another post here How to modify existing XML file with XmlDocument and XmlNode in C# .
here is my code
XmlDocument xml = new XmlDocument();
xml = xml.Load(#"https://www.aade.gr/sites/default/files/2020-09/SampleXML_1.1%20%20%28%CE%A4%CE%99%CE%9C-%CE%A0%CE%A9%CE%9B%CE%97%CE%A3%CE%97%CE%A3_%CE%91%CE%A5%CE%A4%CE%9F%CE%A4%CE%99%CE%9C%29%20.xml");
XmlNodeList aNodes = xml.SelectNodes("/InvoicesDoc/invoice/issuer/vatNumber");
foreach (XmlNode aNode in aNodes)
{
XmlAttribute vatAttribute = aNode.Attributes["vatNumber"];
vatAttribute.Value = "123456789";
}
xml.Save(#"C:\Users\Kostas\Desktop\mydata\infinal.xml");
My problem is that XmlNodeList aNodes will return empty; i have tried to change the xml.SelectNodes("/InvoicesDoc/invoice/issuer/vatNumber") to xml.SelectNodes("/InvoicesDoc/invoice/issuer") and all the way up to single xml.SelectNodes("/InvoicesDoc") but still XmlNodeList aNodes will return empty.
First attempts i loaded the XML doc from file and had the issue. Then i thought maybe something wrong with the file so changed the load of the file directly from the site provides this xml template i need to work on. Both options will load the file fine as i can see it when is saved but my changes will not complete since aNodes is empty and foreach loop will skip straight away.
What am i doing wrong?
thanks for your help in advance.
ps this is the xml i need to edit
https://www.aade.gr/sites/default/files/2020-09/SampleXML_1.1%20%20%28%CE%A4%CE%99%CE%9C-%CE%A0%CE%A9%CE%9B%CE%97%CE%A3%CE%97%CE%A3_%CE%91%CE%A5%CE%A4%CE%9F%CE%A4%CE%99%CE%9C%29%20.xml
Update: I just tried with another xlm found in microsoft example called books on https://learn.microsoft.com/en-us/previous-versions/windows/desktop/ms762271(v=vs.85)
XmlNodelist will also return null/empty when i look for /catalog/book . So the good side is that there is no problem with original xml file i need to edit and the bad side is that still i cannot figure out what i am doing wrong.
XmlNodeList aNodes returns null because the xml contains these namespace declarations:
<InvoicesDoc xmlns=\"http://www.aade.gr/myDATA/invoice/v1.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xsi:schemaLocation=\"http://www.aade.gr/myDATA/invoice/v1.0/InvoicesDoc-v0.6.xsd\"
xmlns:icls=\"https://www.aade.gr/myDATA/incomeClassificaton/v1.0\"
xmlns:ecls=\"https://www.aade.gr/myDATA/expensesClassificaton/v1.0\">
You need to manage your xml doing something like this:
XmlDocument xml = new XmlDocument();
xml.Load(#"https://www.aade.gr/sites/default/files/2020-09/SampleXML_1.1%20%20%28%CE%A4%CE%99%CE%9C-%CE%A0%CE%A9%CE%9B%CE%97%CE%A3%CE%97%CE%A3_%CE%91%CE%A5%CE%A4%CE%9F%CE%A4%CE%99%CE%9C%29%20.xml");
XmlNamespaceManager manager = new XmlNamespaceManager(xml.NameTable);
manager.AddNamespace("InvoicesDoc", "http://www.aade.gr/myDATA/invoice/v1.0");
//Example to get the root element
XmlNodeList root = xml.SelectNodes("/InvoicesDoc:InvoicesDoc", manager);
//Example to get the VatNumber tag
XmlNodeList aNodes =xml.SelectNodes("/InvoicesDoc:InvoicesDoc/InvoicesDoc:invoice/InvoicesDoc:issuer/InvoicesDoc:vatNumber", manager);

All Child Nodes in XML are linked to a Parent Node

I am trying to fix a piece of code that is not doing what I want it to do. The code was left by an engineer who claimed it was working perfectly. He has been using XPATH instead of Linq, so for the short term I'm looking for a solution based on XPATH.
I've searched the web, but am unable to convert what I've found into a working solution.
The XML looks like:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Orders>
<Order>
<OrderId>6175</OrderId>
<OrderNumber>6175</OrderNumber>
<OrderDate>2016-08-19 13:17:41</OrderDate>
<OrderLineItems>
<ItemName>Name of Item</ItemName>
<Quantity>1</Quantity>
<Meta/>
</OrderLineItems>
</Order>
</Orders>
The script he created with Visual Studio 2008 that concerns reading the XML looks like:
public override void CreateNewOutputRows()
{
string filename = Variables.strFileInLoop;
XmlDocument doc = new XmlDocument();
doc.Load(filename);
// loop orders
foreach (XmlNode shipmentNode in doc.DocumentElement.SelectNodes("/Orders/Order"))
{
OutputOrdersBuffer.AddRow();
String OrderId = GetNodeText("OrderId", shipmentNode).Trim();
OutputOrdersBuffer.OrderId = OrderId;
OutputOrdersBuffer.OrderNumber = GetNodeText("OrderNumber", shipmentNode).Trim();
OutputOrdersBuffer.OrderDate = GetNodeText("OrderDate", shipmentNode).Trim();
String replace = #"C:\Projects\Customername\IN\";
OutputOrdersBuffer.FileName = filename.Replace(replace, "");
foreach (XmlNode OrderLineItemNode in shipmentNode.SelectNodes("/Orders/Order/OrderLineItems"))
{
OutputOrderLinesBuffer.AddRow();
OutputOrderLinesBuffer.OrderId = OrderId;
OutputOrderLinesBuffer.ItemName = GetNodeText("ItemName", OrderLineItemNode).Trim();
OutputOrderLinesBuffer.Quantity = GetNodeText("Quantity", OrderLineItemNode).Trim();
OutputOrderLinesBuffer.Meta = GetNodeText("Meta", OrderLineItemNode).Trim();
}
}
}
What happens when we try to import an XML with multiple orders is that the code attaches all OrderLineItems to all Orders.
So if there are 10 unique orders in the file with 2 OrderLineItems it put out 10 Orders with 20 OrderLineItems each.
As I said, I've looked everywhere (I think) but am unable to convert what I've found to a solution where the OrderLineItems are only linked to the order they belong to.
The iteration within an order uses an XPath expression
/Orders/Order/OrderLineItems
that goes through the entire document every time, as the starting / in indicates. This is why all order line items are included in all orders.
Changing the XPath expression to take shipmentNode as the context item, which happens implicitly if the XPath expression does not begin with a /, and navigating from there to its OrderLineItems children should help:
foreach (XmlNode OrderLineItemNode in shipmentNode.SelectNodes("OrderLineItems"))
An analogy with a file system on the command line usually helps when starting with XPath: the context item behaves like a working directory, and one can either change the directory with an absolute path (starting with /), or with a relative path to the current directory (starting with ./, which also works in XPath). Of course XPath is different as it is set-oriented, but this analogy helps getting into the right mindset for navigation.

Remove an XML node by its content

I Have a XML file that I want remove a node that it's content is 'test'.
I search entire Google, so doesn't help me or I can't get my answer.
The XML file content is shown below:
<?xml version="1.0" encoding="utf-8"?>
<parent>
<child>C#</child>
<child>VB</child>
<child>VB.net</child>
<child>F#</child>
<child>C++</child>
<child>C</child>
<child>Ruby</child>
<child>Pascal</child>
<child>test</child>
<child>python</child>
</parent>
I can load it to any list. So my address is OK.
But can't remove the node that its content is test.
One of my tried code is:
XmlDocument doc = new XmlDocument();
doc.Load(Address);
XmlNode node = doc.SelectSingleNode("/parent/child[text()='test']");
doc.ParentNode.RemoveChild(node);
doc.Save(Address);
any suggestion will be appreciable.
Update: Thanks to Andrei Vatasescu the correct answer is:
node.ParentNode.RemoveChild(node);
instead of :
doc.ParentNode.RemoveChild(node);
Actually the program as posted originally works fine, there is just one small error: "ParentNode.RemoveChild()" should be called on node instead of doc.
Try this:
foreach (XmlNode node in doc.ChildNodes)
{
if (node.InnerText == "test")
{
doc.RemoveChild(node);
}
}

Why I can't access to these node content using XPath?

I am pretty new in XPath and in C# and I have the following problem:
I have to parse this file: http://static.nvd.nist.gov/feeds/xml/cpe/dictionary/official-cpe-dictionary_v2.3.xml
As you can see opening it in the browser this file have the following structure:
<?xml version='1.0' encoding='UTF-8'?>
<cpe-list xmlns:meta="http://scap.nist.gov/schema/cpe-dictionary-metadata/0.2" xmlns:config="http://scap.nist.gov/schema/configuration/0.1" xmlns:ns6="http://scap.nist.gov/schema/scap-core/0.1" xmlns:scap-core="http://scap.nist.gov/schema/scap-core/0.3" xmlns="http://cpe.mitre.org/dictionary/2.0" xmlns:cpe-23="http://scap.nist.gov/schema/cpe-extension/2.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://scap.nist.gov/schema/configuration/0.1 http://nvd.nist.gov/schema/configuration_0.1.xsd http://cpe.mitre.org/dictionary/2.0 http://scap.nist.gov/schema/cpe/2.3/cpe-dictionary_2.3.xsd http://scap.nist.gov/schema/scap-core/0.3 http://nvd.nist.gov/schema/scap-core_0.3.xsd http://scap.nist.gov/schema/scap-core/0.1 http://nvd.nist.gov/schema/scap-core_0.1.xsd http://scap.nist.gov/schema/cpe-dictionary-metadata/0.2 http://nvd.nist.gov/schema/cpe-dictionary-metadata_0.2.xsd http://scap.nist.gov/schema/cpe-extension/2.3 http://scap.nist.gov/schema/cpe/2.3/cpe-dictionary-extension_2.3.xsd">
<generator>
<product_name>National Vulnerability Database (NVD)</product_name>
<product_version>2.22.0-SNAPSHOT (PRODUCTION)</product_version>
<schema_version>2.3</schema_version>
<timestamp>2014-03-05T05:13:33.550Z</timestamp>
</generator>
<cpe-item name="cpe:/a:1024cms:1024_cms:0.7">
<title xml:lang="en-US">1024cms.org 1024 CMS 0.7</title>
<cpe-23:cpe23-item name="cpe:2.3:a:1024cms:1024_cms:0.7:*:*:*:*:*:*:*"/>
</cpe-item>
<cpe-item name="cpe:/a:1024cms:1024_cms:1.2.5">
<title xml:lang="en-US">1024cms.org 1024 CMS 1.2.5</title>
<cpe-23:cpe23-item name="cpe:2.3:a:1024cms:1024_cms:1.2.5:*:*:*:*:*:*:*"/>
</cpe-item>
<cpe-item name="cpe:/a:1024cms:1024_cms:1.3.1">
<title xml:lang="en-US">1024cms.org 1024 CMS 1.3.1</title>
<cpe-23:cpe23-item name="cpe:2.3:a:1024cms:1024_cms:1.3.1:*:*:*:*:*:*:*"/>
</cpe-item>
.............................................................
.............................................................
.............................................................
<cpe-item name="cpe:/h:zyxel:p-660hw_t3:v2">
<title xml:lang="en-US">ZyXEL P-660HW T3 Model v2</title>
<cpe-23:cpe23-item name="cpe:2.3:h:zyxel:p-660hw_t3:v2:*:*:*:*:*:*:*"/>
</cpe-item>
</cpe-list>
So now, using XPath, I have to obtain the list of all tag (excluding the first tag situated as first tag into my tag
In my code I have something like it:
XmlDocument document = new XmlDocument(); // Represent an XML document
document.Load(sourceXML.FullName); // Loads the XML document from the specified stream
// Add the namespaces:
XmlNamespaceManager nsmgr = new XmlNamespaceManager(document.NameTable);
nsmgr.AddNamespace("ns6", "http://scap.nist.gov/schema/scap-core/0.1");
nsmgr.AddNamespace("cpe-23", "http://scap.nist.gov/schema/cpe-extension/2.3");
nsmgr.AddNamespace("ns", "http://cpe.mitre.org/dictionary/2.0");
nsmgr.AddNamespace("meta", "http://scap.nist.gov/schema/cpe-dictionary-metadata/0.2");
nsmgr.AddNamespace("scap-core", "http://scap.nist.gov/schema/scap-core/0.3");
nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
nsmgr.AddNamespace("config", "http://scap.nist.gov/schema/configuration/0.1");
/* nodeList is the collection that contains all the <cpe-item> tag that are
* inside the root <cpe-list> tag in the XML document:
*/
XmlNodeList nodeList;
nodeList = document.DocumentElement.SelectNodes("//ns:cpe-list/ns:cpe-item", nsmgr);
long conta = 0;
So I am using this line to select all the tag that are into the tag:
nodeList = document.DocumentElement.SelectNodes("//ns:cpe-list/ns:cpe-item", nsmgr);
It seems to work but I am not sure if it is correct because when I look into using the Visual Studio Debugger it say to me that my XmlNodeList nodeList contains: 80588 element (the file is very big but it seems to me to much element !!!)
Another doubt is related to the use of the ns namespace that is into my previouse code (this is not my code, I have to work on it).
Why in the previous code there is the ns namepace ahead the cpe-list and cpe-item if in the XML code to parse I smply have something like:
<cpe-item name="cpe:/a:1024cms:1024_cms:1.3.1">
<title xml:lang="en-US">1024cms.org 1024 CMS 1.3.1</title>
<cpe-23:cpe23-item name="cpe:2.3:a:1024cms:1024_cms:1.3.1:*:*:*:*:*:*:*"/>
</cpe-item>
that don't begin with ns namespace? Why is it used?
The last question is about how can I access to the title inner text content?
I am trying to do something like this but in this way can't work:
XmlNodeList nodeList;
nodeList = document.DocumentElement.SelectNodes("//ns:cpe-list/ns:cpe-item", nsmgr);
long conta = 0;
DataModel.Vulnerability.CPE currentCPE;
foreach (XmlNode node in nodeList)
{
// Access to the name ATTRIBUTE of the <cpe-item> tag:
Debug.WriteLine(String.Format("[{0:N0}] CPE: {1} Title: {2}", conta, node.Attributes["name"].Value, node.FirstChild.FirstChild.Value));
// Access to the <title> tag content:
//Debug.WriteLine(String.Format("[{0:N0}] Title: {1} Title: {2}", conta, node.SelectSingleNode("./title", nsmgr)));
XmlNode titleNode = node.SelectSingleNode("./title", nsmgr);
conta++;
}
When this code is executed I have no problem to access to the name attributes of the current cpe element into my list but I can't access to the content of the tag because when execute this line:
XmlNode titleNode = node.SelectSingleNode("./title", nsmgr);
it return that the value is null
What is the problem? What am I missing? How can I solve?
Tnx
Andrea
Your XPath looks fine given XML snippet posted in this question. It should return correct number of elements as far as I can see. Can't tell more than that, you should check further yourself.
Your XML has default namespace (xmlns="....."). All elements in XML without prefix considered in default namespace. But in XPath, all element without prefix considered has no namespace. In the end, that different paradigm of both platform requires you to define ns prefix that point to default namespace url for use in XPath statement.
Related to point 2. Remember that all element without prefix is in default namespace. So is <title> element. Hence you need to add ns prefix in the XPath statement : ./ns:title
Ideally, one post has to contains no more than one specific question. Answering a bunch of questions in one post is rarely useful for future visitors, it is tend to confuse them instead. Remember that we are not only solving your problem here, but also trying to build knowledge-base that hopefully useful for others having similar problem.

SelectSingleNode seems to search parents and siblings

I also think I'm confused about XPath usage. I'm new to C# and XPath, so please be patient with me ;)
First, my XML file that I'm testing with:
<?xml version="1.0" encoding="ISO-8859-1"?>
<testroot>
<testnode>
<name>Test Node 1</name>
<things>
<thing>
<number>One</number>
</thing>
<thing>
<number>Two</number>
</thing>
</things>
</testnode>
<testnode>
<name>Test Node 2</name>
<things>
<thing>
<number>Three</number>
</thing>
<thing>
<number>Four</number>
</thing>
</things>
</testnode>
<testnode>
<name>Test Node 3</name>
<things>
<thing>
<number>Five</number>
</thing>
</things>
</testnode>
</testroot>
So first I want to get the "testnode" that contains a "name" I'm interested in. So I did the following, which worked correctly:
XmlNode testRoot = xmlDoc.DocumentElement.SelectSingleNode("/testroot/testnode[name=\"Test Node 1\"]");
Now I want to get all of the nodes under it that contain a "number" element. According to my reading, I should be able to do this:
XmlNodeList testNodes = testRoot.SelectNodes("number");
But that yields an empty list. The only way I got any results was to use //:
XmlNodeList testNodes = testRoot.SelectNodes("//number");
The problem is that seems to search all of the siblings and parents of the testRoot. When I print everything out I get every node in the file that contains "number":
txtOutput.InnerHtml += "<p>" + testRoot.FirstChild.InnerText + "</p>";
foreach (XmlNode node in testNodes)
{
txtOutput.InnerHtml += node.InnerText + "<br />";
}
Test Node 1
One
Two
Three
Four
Five
This behavior confuses me. Am I using XPaths improperly, or does it normally search from the absolute root no matter which XmlNode you start with?
You're trying to find direct child nodes called number. There aren't any of those - just one things element. If you want descendants, say so:
XmlNodeList testNodes = testRoot.SelectNodes("descendant::number");
The version with "//number" basically looks through all nodes in the document, hence your other result.
Having said all this, if you're using .NET 3.5 or higher I'd just use LINQ to XML and do all the querying in that. It's a much nicer API, IMO :)

Categories