Order XmlNodeList based on an attribute - c#

I have an XmlNodeList that contains packets (item) from the root of the XML example below. I want to sort the XmlNodeList based on the node's key attribute value.
The sorting has to be very efficient, every millisecond counts.
Do you have any idea?
<root>
<item key="1000000020">
Content 20
</item>
<item key="1000000001">
Content 1
</item>
...
<item key="1043245231">
Content n
</item>
</root>
Edit:
I already have an XmlNodeList constructed from the items. I do not have access to the XmlDocument anymore, only the list of items.

You should try Linq to XML.
XDocument doc = XDocument.Load(file);
var nodeList = from ele in doc.Descendants("item")
orderby int.Parse(ele.Attribute("key").Value)
select ele;
You may try XPathNavigator and XPathExpression.
//I presume that variable xNodeList contains XmlNodeList.
XPathNavigator nav=xNodeList.Item(0).OwnerDocument.CreateNavigator();
XPathExpression exp = nav.Compile("root/item");
exp.AddSort("#key", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Number );
foreach (XPathNavigator t in nav.Select(exp))
{
Console.WriteLine(t.OuterXml );
}

note: xml variable is string value
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
IEnumerable<XmlNode> rows = doc.SelectNodes("report/table/row").Cast<XmlNode>().OrderByDescending(r => Convert.ToDecimal(r.Attributes["conversions"].Value));

I solved the problem in a very non-elegant way:
I iterated my XmlNodeList
During iteration I extracted the timestamps
After extracting a timestamp I added the timestamp-XmlElement to a SortedDictionary
Converted the SortedDictionary to list (sortedKeys = sortedByDateDisctionary.Keys.ToList();)
If the nodes need to be sorted Descending then sortedKeys.Reverse();
Then the nodes can be accessed by the sorted keys

Related

How to get nodes from XML file without its attribute and put to a List of string

I would like to display the tag names of child nodes without its attributes. Then those tag names (nodes) should be put in a List of string. Here's example of my XML file:
<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
<CAR>
<ID>21</ID>
<MANUFACTURER>Ford</MANUFACTURER>
<MODEL>Fiesta</MODEL>
</CAR>
<CAR>
<ID>22</ID>
<MANUFACTURER>Peugeot</MANUFACTURER>
<MODEL>508</MODEL>
</CAR>
</ROOT>
So, the effect I want to get in a console output is shown below:
ID
MANUFACTURER
MODEL
Then I would like to store that ID, MANUFACTURER and MODEL tag names in a List of strings.
This is the code that I tried so far:
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.PreserveWhitespace = true;
try
{
xmlDocument.Load("XMLFile.xml");
}
catch (FileNotFoundException ex)
{
Console.WriteLine(ex);
}
Console.WriteLine(xmlDocument.OuterXml);
XmlNodeList nodeList = xmlDocument.SelectNodes("ROOT/CAR");
foreach(XmlNode node in nodeList)
{
Console.WriteLine(node.ChildNodes);
xmlNodes.Add(node.ChildNodes.ToString());
}
The problem is that it's not displaying the way I want to. As a result I only get two System.Xml.XmlChildNodes which seems to be corresponding to two <CAR> nodes, instead of its three child nodes, such as ID, MANUFACTURER and MODEL.
System.Xml.XmlChildNodes
System.Xml.XmlChildNodes
Adding items to a List basically adds the same thing as shown above.
What am I doing wrong?
If you have to use XmlDocument, then you can -
List<string> elements = new List<string>();
XmlNodeList CarNodes = xml.SelectNodes("Root/Car");
foreach(XmlNode c in CarNodes)
{
foreach(XmlNode n in c.ChildNodes)
{
if (!elements.Contains(n.Name))
{
elements.Add(n.Name);
}
}
}
But I find XDocument to be much simpler and better readability.
XDocument xdoc = XDocument.Parse(yourXmlString);
List<string> elements = xdoc.Descendants("Car")
.DescendantNodes().OfType<XElement>()
.Select(x => x.Name).Distinct().ToList();
And thats all you'll need. Easy to read as well, get all the descendants of "Car" Node and get all distinct names of XElements within it.
Another way to do it -
List<string> elements = xdoc.Descendants("Car").First()
.DescendantNodes().OfType<XElement>()
.Select(x => x.Name).ToList();
In this case I have removed the "distinct" and rather got just the first Car node ONLY. You can see the difference - if by any case some other Car node has an extra element, you'll miss getting that information by doing it this way.
You could loop through for children nodes:
1- You can define xmlNodes like a HashSet to avoid multiple tags like :
HashSet<string> xmlNodes = new HashSet<string>();
2 - Change little the code like :
....
XmlNodeList nodeList = xmlDocument.SelectNodes("ROOT/CAR");
foreach (XmlNode node in nodeList)
{
foreach(XmlNode element in node.ChildNodes)
{
if (element.NodeType == XmlNodeType.Element)
xmlNodes.Add(element.Name);
}
}
Demo
Console.WriteLine(string.Join(", ", xmlNodes));
Result
ID, MANUFACTURER, MODEL
I hope you find this helpful.

Searching through XML to find a list of items

I have an xml doc as such:
<?xml version="1.0" encoding="utf-8" ?>
<Categories>
<Category>
<Name>Fruit</Name>
<Items>
<Item>Apple</Item>
<Item>Banana</Item>
<Item>Peach</Item>
<Item>Strawberry</Item>
</Items>
</Category>
<Category>
<Name>Vegetable</Name>
<Items>
<Item>Carrots</Item>
<Item>Beets</Item>
<Item>Green Beans</Item>
<Item>Bell Pepper</Item>
</Items>
</Category>
<Category>
<Name>Seafood</Name>
<Items>
<Item>Crab</Item>
<Item>Lobster</Item>
<Item>Shrimp</Item>
<Item>Oysters</Item>
<Item>Salmon</Item>
</Items>
</Category>
</Categories>
I would like to be able to search on a term such as Category.Name = Fruit and get back the list of the Fruit Items.
Here is the incomplete code I've started so far:
string localPath = Server.MapPath("~/App_Data/Foods.xml");
XmlDocument doc = new XmlDocument();
doc.Load(localPath);
XmlNodeList list = doc.SelectNodes("Categories");
//Do something here to search the category names and get back the list of items.
This is my first attempt at parsing through XML so I'm a bit lost. Note: the application I am working on uses .Net 2.0
I'd suggest to read about XPath as you're limited to .NET 2.0, moreover XPath is very useful to work with XML even in more general context (not limited to .NET platform only).
In this particular case XPath become useful because SelectNodes() and SelectSingleNode() method accept XPath string as parameter. For example, to get all <Item> that corresponds to category name "Fruit" :
string localPath = Server.MapPath("~/App_Data/Foods.xml");
XmlDocument doc = new XmlDocument();
doc.Load(localPath);
XmlNodeList items = doc.SelectNodes("Categories/Category[Name='Fruit']/Items/Item");
foreach(XmlNode item in items)
{
Console.WriteLine(item.InnerText);
}
You can see XPath as a path, similar to file path in windows explorer. I'd try to explain only the bit that is different from common path expression in the above sample, particularly this bit :
...\Category[Name='Fruit']\...
the expression within square-brackets is a filter which say search for <Category> node having child node <Name> equals "Fruit".
You are on the right path. However, you would need to load the 'Categories' node first, then you can get it's child nodes.
I have added a filter to return only nodes where the name is "Fruit".
XmlNode cat = doc.SelectSingleNode("Categories");
var list = cat.SelectNodes("Category").Cast<XmlNode>()
.Where(c => c.SelectSingleNode("Name").InnerText == "Fruit");
foreach ( XmlNode item in list )
{
// process each node here
}

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

Extract nodes from XmlNodeList, with namespace, and where same child appears at multiple levels

I am trying to extract those subnodes, but I had got just headache so far...
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<supplyCrew xmlns="http://site.ddf.com">
<login>
<login>XXXX</login>
<password>XXXX</password>
</login>
<flightInformation>
<flights>
<item>
<arrivalDateTime>2010-11-08T22:48:00.000Z</arrivalDateTime>
<arrivingCity>ORD</arrivingCity>
<crewMembers>
<item>
<employeeId>020040</employeeId>
<isDepositor>Y</isDepositor>
<isTransmitter>N</isTransmitter>
</item>
<item>
<employeeId>09000</employeeId>
<isDepositor>N</isDepositor>
<isTransmitter>Y</isTransmitter>
</item>
</crewMembers>
</item>
<item>
<arrivalDateTime>2010-11-08T20:29:00.000Z</arrivalDateTime>
<arrivingCity>JFK</arrivingCity>
<crewMembers>
<item>
<employeeId>0538</employeeId>
<isDepositor>Y</isDepositor>
<isTransmitter>N</isTransmitter>
</item>
<item>
<employeeId>097790</employeeId>
<isDepositor>N</isDepositor>
<isTransmitter>Y</isTransmitter>
</item>
with the code I can get them, but I do not know how to select each one according to their tag name to insert them into a database.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("C:/Crew_Request_Sample.xml");
XmlNodeList elemList = xmlDoc.GetElementsByTagName("item");
foreach (XmlNode node in elemList)
{
Debug.WriteLine(node.InnerText);
}
I need some direction, please.
The problem with using GetElementsByTagName("item") here is that there are 2 levels of item node - one as a child of flights and another item as a child of crewMembers.
Edit Now that the full xml is pasted, it is clear that there is also a namespace involved as well. To handle namespaces, make use of a namespace manager to define aliases for the namespaces, which you can then use in the xpath queries:
var nsm = new XmlNamespaceManager(xmlDoc.NameTable);
nsm.AddNamespace("s", "http://site.ddf.com");
var elemList = xmlDoc.SelectNodes("//s:crewMembers/s:item", nsm);
foreach (var node in elemList)
{
Debug.WriteLine(node.SelectSingleNode("s:employeeId", nsm).InnerText);
Debug.WriteLine(node.SelectSingleNode("s:isDepositor", nsm).InnerText);
Debug.WriteLine(node.SelectSingleNode("s:isTransmitter", nsm).InnerText);
}
You can do it using LINQ2XML..
XElement doc=XElement.Load("C:/Crew_Request_Sample.xml");
XNamespace e = "http://schemas.xmlsoap.org/soap/envelope/";
XNamespace s = "http://site.ddf.com";
//this would access the nodes of item->crewMembers->item and put it into an Anonymous Type
var yourList=doc.Descendants(e+"Body")
.Descendants(s+"supplyCrew")
.Descendants(s+"flightInformation")
.Descendants(s+"flights")
.Descendants(s+"item")
.Descendants(s+"crewMembers")
.Descendants(s+"item")
.Select(
x=>new
{
//Anonymous Type
employeeId=x.Element(s+"employeeId").Value,
isDepositor=x.Element(s+"isDepositor").Value,
isTransmitter=x.Element(s+"isTransmitter").Value
}
);
You can then access yourList using for-each loop
foreach(var item in yourList)
{
Console.WriteLine(item.employeeId);
Console.WriteLine(item.isDepositor);
Console.WriteLine(item.isTransmitter);
}
I think you'll do it faster and easily using this technique
Linq To XML
There is a lot of examples in the site, so it 'll be easy to find what you want.
Hope it helps.

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

Categories