Maximum number of attributes a node has in a XML document - c#

We are interested in finding maximum number of attributes a node has in a XML document. My code is below using C#:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(#"C:\ABC.xml");
XmlNode root = xmlDoc.DocumentElement;
int nodeAttrCount = 0;
foreach (XmlNode node in root)
if (nodeAttrCount < node.Attributes.Count)
nodeAttrCount = node.Attributes.Count;
We are interested is: do we have any thing better than this. Like any method or property which give us the same result or anyother option.

You can also use LINQ to XML:
XElement el = XElement.Load("MyXML.xml");
int maxAttr = el.DescendantNodesAndSelf().OfType<XElement>().Max(x => x.Attributes().Count());
The above code traverses all the xml nodes (it works with nested nodes too) and get the maximum number of attributes.
For .net 2.0:
XmlDocument doc = new XmlDocument();
doc.Load("MyXML.xml");
int max = 0;
foreach (XmlNode xmlNode in doc.SelectNodes("//*"))
if (max < node.Attributes.Count)
max = node.Attributes.Count;
This is basically the same as your solution;
the main difference is that it considers all nodes at every nesting level (using XPath navigation).

This is three lines of code for a fairly niche requirement. I wouldn't expect this to already exist in the .NET framework.
Your foreach loop looks fine. Are you sure you want to only look at the root elements, and not recurse inside the document?

Related

how to programmatically add XmlNode to an XmlNodeList

I have an XmlNodeList of products whose values are put into a table. Now I want to add a new XmlNode to the list when a certain product is found so that in the same loop the new products is treated the same as the items that are originally in the file. This way the structire of the function does not need to change, just add an extra node that is processed next.
But an XmlNode is an abstract class and I cant figure out how to create the new node programatically. Is this possible?
XmlNodeList list = productsXml.SelectNodes("/portfolio/products/product");
for (int i = 0; i < list.Count; i++)
{
XmlNode node = list[i];
if (node.Attributes["name"].InnertText.StartsWith("PB_"))
{
XmlNode newNode = ????
list.InsertAfter(???, node);
}
insertIntoTable(node);
}
XmlDocument is the factory for its nodes so you have to do this:
XmlNode newNode = document.CreateNode(XmlNodeType.Element, "product", "");
Or its shortcut:
XmlNode newNode = document.CreateElement("product");
Then to add newly created node to its parent:
node.ParentNode.AppendChild(newNode);
If added node must be processed then you have to do it explicitly: node list is a snapshot of nodes that matched search condition then it won't be dynamically updated. Simply call insertIntoTable() for added node:
insertIntoTable(node.ParentNode.AppendChild(newNode));
If your code is much different you may need little bit of refactoring to make this process a two step batch (first search for nodes to add and then process them all). Of course you may even follow a completely different approach (for example copying nodes from XmlNodeList to a List and then adding them to both lists).
Assuming this is enough (and you do not need refactoring) let's put everything together:
foreach (var node in productsXml.SelectNodes("/portfolio/products/product"))
{
if (node.Attributes["name"].InnertText.StartsWith("PB_"))
{
XmlNode newNode = document.CreateElement("product");
insertIntoTable(node.ParentNode.AppendChild(newNode));
}
// Move this before previous IF in case it must be processed
// before added node
insertIntoTable(node);
}
Refactoring
Refactoring time (and if you have a 200 lines function you really need it, much more than what I present here). First approach even if not very efficient:
var list = productsXml
.SelectNodes("/portfolio/products/product")
.Cast<XmlNode>();
.Where(x.Attributes["name"].InnertText.StartsWith("PB_"));
foreach (var node in list)
node.ParentNode.AppendChild(document.CreateElement("product"));
foreach (var node in productsXml.SelectNodes("/portfolio/products/product"))
insertIntoTable(node); // Or your real code
If you do not like a two passes approach you may use ToList() like this:
var list = productsXml
.SelectNodes("/portfolio/products/product")
.Cast<XmlNode>()
.ToList();
for (int i=0; i < list.Count; ++i)
{
var node = list[i];
if (node.Attributes["name"].InnertText.StartsWith("PB_"))
list.Add(node.ParentNode.AppendChild(document.CreateElement("product"))));
insertIntoTable(node);
}
Please note that in second example the use of for instead of foreach is mandatory because you change the collection within the loop. Note that you may even keep your original XmlNodeList object in place...
You can't create an XmlNode directly, but only the subtypes (e.g. elements) by using one of the Create* methods of the parent XmlDocument. E.g. if you want to create a new element:
XmlElement el = doc.CreateElement("elementName");
node.ParentNode.InsertAfter(el, node);
Please note that this will not add the element to the XmlNodeList list so the node won't be processed. All necessary processing of the node should therefore be done right when adding the node.

How to get all XML nodes with the same name without knowing their level?

I have a XML Example:
<Fruits>
<Red_fruits>
<Red_fruits></Red_fruits>
</Red_fruits>
<Yellow_fruits>
<banana></banana>
</Yellow_fruits>
<Red_fruits>
<Red_fruits></Red_fruits>
</Red_fruits>
</Fruits>
I have 4 Red_fruits tags, 2 of them shares the same ParentNode (Fruits), I want to get those which have the same ParentNode.
But I just want those which have the same name (Red_fruits), which means Yellow_fruits tag isn't included.
This is the way I am doing right now using C# language:
XmlDocument doc = new XmlDocument();
string selectedTag = cmbX.text;
if (File.Exists(txtFile.text))
{
try
{
//Load
doc.Load(cmbFile.text);
//Select Nodes
XmlNodeList selectedNodeList = doc.SelectNodes(".//" + selectedTag);
}
Catch
{
MessageBox.show("Some error message here");
}
}
This is returning me all red_fruits, not just the ones that belongs to Fruits.
I can't make XmlNodeList = doc.SelectNodes("/Fruits/Red_fruits") because I want to use this code to read random XML files, so I don't know the exact name that specific node will have, I just need to put all nodes with the same name and same level into a XmlNodeList using C# Language.
Is there a way of achieve this without using LINQ? How to do that?
An understanding on the usage of Single Slash / and Double slash // can help here.
Let's see how / and // work in relation to the root node. When / is used at the beginning of a path:
/a
it will define an absolute path to node a relative to the root. As such, in this case, it will only find a nodes at the root of the XML tree.
When // is used at the beginning of a path:
//a
it will define a path to node a anywhere within the XML document. As such, in this case, it will find a nodes located at any depth within the XML tree.
These XPath expressions can also be used in the middle of an XPath value to define ancestor-descendant relationships. When / is used in the middle of a path:
/a/b
it will define a path to node b that is an immediate direct descendant (ie. a child) of node a.
When // used in the middle of a path:
/a//b
it will define a path to node b that is ANY descendant of node a.
Coming back to your question:
// using GetElementsByTagName() return all the Elements having name: Red_Fruits
XmlDocument doc = new XmlDocument();
XmlNodeList nodes= doc.GetElementsByTagName("Red_Fruits");
//Using SelectNodes() method
XmlNodelist nodes = doc.SelectNodes("//Fruits/Red_Fruits");
// This will select all elements that are children of the <Fruits> element.
In case <Fruits> is the root element use the Xpath: /Fruits/Red_Fruits. [ a single slash /]
If you're simply trying to find the "next" or "previous" iteration of a single node, you can do the following and then compare it to the name
XmlNode current = doc.SelectSingleNode("Fruits").SelectSingleNode("Red_fruits");
XmlNode previous = current.NextSibling;
XmlNode next = current.NextSibling;
and you can iterate until you find the proper sibling
while(next.Name != current.Name)
{
next = next.NextSibling;
}
or you can even get your list by invoking the 'Parent' property
XmlNodeList list = current.ParentNode.SelectNodes(current.Name);
Worst case scenario, you can cycle through the XMLNode items in selectedNodeList and check the ParentNode properties. If necessary you could go recursive on the ParentNode check and count the number of times it takes to get to the root node. This would give you the depth of a node. Or you could compare the ParentNode at each level to see if it is the parent you are interested in, if that parent is not the root.
public void Test(){
XmlDocument doc = new XmlDocument();
string selectedTag = cmbX.text;
if (File.Exists(txtFile.text))
{
try
{
//Load
doc.Load(cmbFile.text);
//Select Nodes
XmlNodeList selectedNodeList = doc.SelectNodes(".//" + selectedTag);
List<XmlNode> result = new List<XmlNode>();
foreach(XmlNode node in selectedNodeList){
if(depth(node) == 2){
result.Add(node);
}
}
// result now has all the selected tags of depth 2
}
Catch
{
MessageBox.show("Some error message here");
}
}
}
private int depth(XmlNode node) {
int depth = 0;
XmlNode parent = node.ParentNode;
while(parent != null){
parent = node.ParentNode;
depth++;
}
return depth;
}

stuck on xpath query

I have an xmlnodelist which results in the below snippet
<updi:ProductName xmlns:updi="urn:rosettanet:specification:universal:ProductIdentification:xsd:schema:01.04">Packet Processing Card (PPC) 16GB</updi:ProductName>
<ulc:AlternativeIdentifier xmlns:ulc="urn:rosettanet:specification:universal:Locations:xsd:schema:01.04">
<ulc:Authority>PID</ulc:Authority>
<ulc:Identifier>ASR5K-PPC-K9=</ulc:Identifier>
</ulc:AlternativeIdentifier>
<ulc:AlternativeIdentifier xmlns:ulc="urn:rosettanet:specification:universal:Locations:xsd:schema:01.04">
<ulc:Authority>CPN</ulc:Authority>
<ulc:Identifier />
</ulc:AlternativeIdentifier>
How can I grab the two authority and identifier tags? I tried adding a root element but I have issues with the namespaces. The original namespace declaration is very large.
Well, I doubt there's anything wrong with the XPath queries you've tried - more likely it's the namespaces that are tripping you up. There are a few ways to compensate/deal with this, ranging from "strip out all the namespaces" to using the XmlNamespaceManager - here's an example of that:
void Main()
{
var doc = new XmlDocument();
var namespaceMgr = new XmlNamespaceManager(doc.NameTable);
namespaceMgr.AddNamespace("updi", "urn:rosettanet:specification:universal:ProductIdentification:xsd:schema:01.04");
namespaceMgr.AddNamespace("ulc", "urn:rosettanet:specification:universal:Locations:xsd:schema:01.04");
doc.LoadXml(xml);
var authorityTags = doc.SelectNodes("//ulc:Authority", namespaceMgr);
var identifierTags = doc.SelectNodes("//ulc:Identifier", namespaceMgr);
}
Assuming xml is:
string xml = #"
<ROOT>
<updi:ProductName xmlns:updi=""urn:rosettanet:specification:universal:ProductIdentification:xsd:schema:01.04"">
Packet Processing Card (PPC) 16GB
</updi:ProductName>
<ulc:AlternativeIdentifier xmlns:ulc=""urn:rosettanet:specification:universal:Locations:xsd:schema:01.04"">
<ulc:Authority>PID</ulc:Authority>
<ulc:Identifier>ASR5K-PPC-K9=</ulc:Identifier>
</ulc:AlternativeIdentifier>
<ulc:AlternativeIdentifier xmlns:ulc=""urn:rosettanet:specification:universal:Locations:xsd:schema:01.04"">
<ulc:Authority>CPN</ulc:Authority>
<ulc:Identifier />
</ulc:AlternativeIdentifier>
</ROOT>";
The below will return all four nodes from your sample. I did have to wrap it in a root node for my testing. The | operator allows for the union functionality.
//ulc:AlternativeIdentifier/ulc:Authority | //ulc:AlternativeIdentifier/ulc:Identifier
I tested this using Notepad++ with the XPatherizerNPP plugin, which I highly recommend.
Something like this will do the trick:
XmlDocument doc = new XmlDocument();
doc.Load("YourXmlFile");
foreach (XmlNode node in doc.SelectNodes("//*[local-name() = \"Authority\"]"))
{
Console.WriteLine("Authority: " + node.InnerText);
}
foreach (XmlNode node in doc.SelectNodes("//*[local-name() = \"Identifier\"]"))
{
Console.WriteLine("Identifier: " + node.InnerText);
}
Basically, SelectNodes("//*[local-name() = \"Identifier\"]") tells it to search for node in the xml with that name, regardless of namespace, etc.

Xdocument, get each element(s) value

I have xml as follows:
<Reports>
<report>
<name>By Book</name>
<report_type>book</report_type>
<Object>Count Change</Object>
<Slicers detail="detail">
<Namespace>EOD</Namespace>
<BookNode>HighLevel</BookNode>
<DateFrom>T-2</DateFrom>
<DateTo>T-1</DateTo>
<System>NewSystem</System>
</Slicers>
</report>
</Reports>
I simply want to loop through the value of each element of the Xdocument (pref would be any element under Slicers) but to start with just all elements.
When I run the following:
var slicers = from c in config.Elements("Reports")
select c.Value ;
foreach (var xe in slicers)
{
Console.WriteLine(xe);
}
The output is a single line concatenating all the values together.
"By BookbookCount ChangeEODHighLevelT-2T-1NewSystem"
I want to loop through them one at a time, 'By Book' first, run some code then book etc etc.
I am sure this is simple, but cant get round it. I have tried foreach(Xelement in query) but same resulst
i would do it something like this;
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
//load in your xml here
XmlNodeList xnList = doc.SelectNodes("nodeYou'reLookingFor");
//for getting just the splicers you could do "Reports/report/Slicers"
foreach (XmlNode node in xnList)
string namespace = node["Namespace"].InnerText;
//go through all your nodes here
you're creating a xmldoc, loading your xml into it, creating a list which holds each node in the list (at a specified Xpath), and then looping through each. in the loop you can do whatever you want by referencing
node["nodenamehere"].InnerText

Get xml node using c#

I have a request that returns a large xml file. I have the file in a XmlDocument type in my application. From that Doc how can I read an element like this:
<gphoto:videostatus>final</gphoto:videostatus>
I would like to pull that value final from that element. Also If i have multiple elements as well, can I pull that into a list? thanks for any advice.
If you already have an XmlDocument then you can use the function GetElementsByTagName() to create an XmlNodeList that can be accessed similar to an array.
http://msdn.microsoft.com/en-us/library/dc0c9ekk.aspx
//Create the XmlDocument.
XmlDocument doc = new XmlDocument();
doc.Load("books.xml");
//Display all the book titles.
XmlNodeList elemList = doc.GetElementsByTagName("title");
for (int i=0; i < elemList.Count; i++)
{
Console.WriteLine(elemList[i].InnerXml);
}
You can select nodes using XPath and SelectSingleNode SelectNodes. Look at http://www.codeproject.com/Articles/9494/Manipulate-XML-data-with-XPath-and-XmlDocument-C for examples. Then you can use for example InnerText to get final. Maybe you need to work with namespaces (gphoto). The //videostatus would select all videostatus elements
You can try using LINQ
XNamespace ns = XNamespace.Get(""); //use the xmnls namespace here
XElement element = XElement.Load(""); // xml file path
var result = element.Descendants(ns + "videostatus")
.Select(o =>o.Value).ToList();
foreach(var values in value)
{
}
Thanks
Deepu

Categories