C# ASP.NET Sort XML Nodes by custom attribute - c#

Is there a way we can sort xmlnodes based on attribute values? The point is that every child node has a different name, despite it, I want to sort them by attribute.
E.g.
<Doc>
<mar_03 data="03">
<Mattina_Turno_1 />
</mar_03>
<dom_01 data="01">
<Mattina_Turno_1 />
</dom_01>
<mer_04 data="04">
<Mattina_Turno_1 />
<Mattina_Turno_2 />
</mer_04>
</Doc>
Should become
<Doc>
<dom_01 data="01">
<Mattina_Turno_1 />
</dom_01>
<mar_03 data="03">
<Mattina_Turno_1 />
</mar_03>
<mer_04 data="04">
<Mattina_Turno_1 />
<Mattina_Turno_2 />
</mer_04> </Doc>
How can I do it? After sorting obviously I want to overwrite the file.
This answer does not fix my problem since i can not define the node "item" since every my nodes are named differently.
Thanks, and please do not mark it as duplicate, because it is not!

Please try,
XDocument xdoc = XDocument.Load("File.xml");
var result = xdoc.Element("Doc")
.Elements()
.OrderBy(s => (int)s.Attribute("data"));
string xmlOutPut = string.Empty;
result.ToList().ForEach(a =>
{
xmlOutPut += a;
});
Where data and Doc is the parent element is your attribute according to your example. File.xml is your xml file name. You will get the sorted output in 'xmlOutPut'
or everything in a single Linq query,
XDocument xdoc = XDocument.Load("XMLFile2.xml");
string xmlOutPut = string.Empty;
xdoc.Element("Doc")
.Elements()
.OrderBy(s => (int)s.Attribute("data"))
.ToList().ForEach(a =>
{
xmlOutPut += a;
});

Sorting
XDocument xDoc = XDocument.Load("FileName.xml");
var res = xDoc.Element("Doc")
.Elements()
.OrderByDescending(c => (int) c.Attribute("data"));
Then to save it:
XDocument doc = new XDocument(new XElement("Doc", res));
doc.Save("FileName.xml");

Related

Getting XML node based on text value, that ends with XML

I have this xml document, where I want to read documenturl where innertext ends with .xml
<Root>
<hits>
<total>4</total>
<max_score />
<hits>
<_index>offentliggoerelser-prod-20161006</_index>
<_type>offentliggoerelse</_type>
<_id>urn:ofk:oid:23640715</_id>
<_score />
<_source>
<dokumenter>
<dokumentUrl>url.pdf</dokumentUrl>
<dokumentMimeType>application/pdf</dokumentMimeType>
<dokumentType>KONCERNREGNSKAB_FOR_OVERLIGGENDE_MODER</dokumentType>
</dokumenter>
<dokumenter>
<dokumentUrl>url.xml</dokumentUrl>
<dokumentMimeType>application/xml</dokumentMimeType>
<dokumentType>AARSRAPPORT</dokumentType>
</dokumenter>
<dokumenter>
<dokumentUrl>url.pdf</dokumentUrl>
<dokumentMimeType>application/pdf</dokumentMimeType>
<dokumentType>AARSRAPPORT</dokumentType>
</dokumenter>
</_source>
<sort>1490355849989</sort>
</hits>
</hits>
</Root>
I am trying to read dokumentUrl that ends with '.xml' only.
I try to use LINQ
XDocument mydoc = XDocument.Load(file);
XDocument xPlatformXml = new XDocument(mydoc);
XElement xel = xPlatformXml.Element("_source")
.Elements("dokumenter")
.Where(x => x.Element("dokumentUrl").Value == ".xml")
.SingleOrDefault();
but it does not work, can anyone pls help me
You can check if you have the descendants node that has value ends with .xml
XDocument mydoc = XDocument.Load(file);
XDocument xPlatformXml = new XDocument(mydoc);
XElement xel = xPlatformXml.Root.Descendants()
.Where(x => x.Name == "dokumentUrl" && x.Value.EndsWith(".xml"))
.SingleOrDefault();
Try this:
XDocument xPlatformXml = new XDocument(mydoc);
XElement xel = xPlatformXml.Element("_source")
.Elements("dokumenter")
.Where(x => x.Element("dokumentUrl").Value.EndsWith(".xml"))
.SingleOrDefault();
When using the Element method you must specify the entire path from the document root to the desired nodes. For example:
var xel = xPlatformXml.Element("Root")
.Element("hits")
.Element("hits")
.Element("_source")
.Elements("dokumenter")
.Elements("dokumentUrl")
.SingleOrDefault(x => x.Value.EndsWith(".xml"));
Or you can use the Descendants method. It is more concise, but may affect performance.
var xel2 = xPlatformXml.Descendants("dokumentUrl")
.SingleOrDefault(x => x.Value.EndsWith(".xml"));

use LINQ on XmlNodeList

<X version="1.0">
<Y id="abc" abv="a"/>
<Y id="edf" abv="e"/>
</X>
I want to select the node whose id is "abc", and return its abv "a".
XmlDocument doc = new XmlDocument();
doc.Load(filePath);
XmlNodeList list = doc.SelectNodes("X/Y");
var node = list.Cast<XmlNode>().Where(node => node["id"].InnerText == "abc")
.Select(x=>x["abv"].InnerText);
But it does't work, node["id"].InnerText is always "". Can you point out where is a problem?
Thanks a lot
Aside from the fact what your code snippet wouldn't be compiled because of non-unique node variable (first outside of linq query and second in "where" method lambda), you have also missed Attributes in your query.
It should be something like
var node = list.Cast<XmlNode>()
.Where(n => n.Attributes["id"].InnerText == "abc")
.Select(x => x.Attributes["abv"].InnerText);
The InnerText for a node is the text that appears between <node> and </node>. So for, eg <Y attributes /> there is no inner text.
You need to use node => node.Attributes["id"].Value == "abc"
Just cast XmlNodeList to List, like that:
List<XmlNode> list = new List<XmlNode>();
foreach(XmlNode a in xmlNodeList)
{
list.Add(a);
}
list.OrderBy((element) => element.ChildNodes[0].InnerText);

Use LINQ XML with a namespace

I am trying to find nodes in an XML document like this:
<?xml version="1.0"?>
<TrainingCenterDatabase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2">
<Activities>
<Activity Sport="CyclingTransport">
<Id>2014-07-08T15:28:14Z</Id>
</Activity>
</Activities>
</TrainingCenterDatabase>
I aim to extract the node value 'Id' with code like this:
XDocument doc = XDocument.Load(filePath);
List<string> urlList = doc.Root.Descendants("Id")
.Select(x => (string)x)
.ToList();
Console.WriteLine(urlList.Count);
However the count is 0, where I expect 1.
After some debugging and editing the XML I noticed that if I change the TrainingCenterDatabase node and remove the attributes to this:
<TrainingCenterDatabase>
Then the result is a count of 1 as expected.
So my question is how do I take into account the namespaces so that I can get the value when the TrainingCenterDatabase node has these attributes?
Namespaces in XML can be tricky. I've run into this problem myself a number of times. In all likelihood, the following will fix your problem:
XDocument doc = XDocument.Load(filePath);
List<string> urlList = doc.Root.Descendants(doc.Root.Name.Namespace.GetName("Id"))
.Select(x => (string)x)
.ToList();
Console.WriteLine(urlList.Count);
Basically, this just assumes the underlying element to have the same namespace as your root element. That's true in this case, but of course it doesn't have to be.
The right way, probably, is to do it explicitly. Now, granted, that kind of depends on how you're using this and your datasource, so make the decision for yourself, but that would require doing something more like this:
XDocument doc = XDocument.Load(filePath);
List<string> urlList = doc.Root.Descendants(System.Xml.Linq.XName.Get("Id", "http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2"))
.Select(x => (string)x)
.ToList();
Console.WriteLine(urlList.Count);
The cause for your problem was that the default behavior for XElement, when not given an explicit namespace, is to assume no namespace. However, the default behavior for the XML spec is to assume the parent's namespace. In your case, those two were different, so it wasn't able to find the descendant.
It Works...
XDocument doc = XDocument.Load(filePath);
XNamespace ns = "http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2";
var root = doc.Descendants(ns + "Id").Select(x => x.Value).ToList();
Console.WriteLine(root.Count);

How to get a specific XML value from an XML using C#?

I have the below XML as:
<Requests xmlResponse="true">
<Request response="yes" responsewait="120000" sequence="1" type="Fulfillment">
<RequestData>
<PrintControl>FTP</PrintControl>
<User>81DF</User>
<Documents>
<AddressChangeLetter>
<DocumentInfo>
<AddressChange AddressChangeId="109346" Branch="418" LastChangeDate="">
<Name>AAA NOVAK</Name>
<TaxID>123123121</TaxID>
<OldAddress1>BOX 216</OldAddress1>
<OldAddress2>NYANE 68017</OldAddress2>
<OldAddress3 />
<OldAddress4 />
<NewAddress1>P O BOX 216</NewAddress1>
<NewAddress2>CERESCO NE 68017</NewAddress2>
<NewAddress3 />
<NewAddress4 />
<DateChanged>05/08/2013</DateChanged>
<AccountInfo AcctNum="231232311" AcctStatusCodes="IX" />
</AddressChange>
</DocumentInfo>
</AddressChangeLetter>
</Documents>
</RequestData>
I wanted to get the name or the value which is under the tag "Documents". Since in the above XML, the tag under the "Document" tag is "AddressChangeLetter", therefore, I want to get this name. How will I do it.
Something along the lines of... (it's not perfect, but it'll get you started - Google the functions I've used to get it working properly):
XmlDocument xml = new XmlDocument();
xml.Load(yourPathGoesHere)
XmlNodeList addressNodes = xml.GetElementsByTagName("AddressChange");
foreach (XmlNode oneNode in addressNodes) {
myVariableToGrabNames = oneNode["Name"].InnerText;
}
This can be done pretty easily using Linq to XML e.g.
var xml = ...;
var xdoc = XDocument.Parse(xml);
foreach (var e in xdoc.Descendants("Documents").Elements())
{
var name = e.Name; // AddressChangeLetter
}

Linq to XML select distinct value from

I am working with sharePoint Lists webservice, in order to load a dropdown in search page I need to extract all the "ows_Country" name from XML, returend in XMLNode in the Format of :
<rs:data ItemCount="1" xmlns:rs="urn:schemas-microsoft-com:rowset">
<z:row ows_Title="Nike" ows_ID="1" ows_Country="Spain" xmlns:z="#RowsetSchema" />
<z:row ows_Title="Addidas" ows_ID="4" ows_Country="Brazil" xmlns:z="#RowsetSchema" />
<z:row ows_Title="Puma" ows_ID="5" ows_Country="Spain" xmlns:z="#RowsetSchema" />
</rs:data>
I need to use LINQ to get the distinct "ows_Country" from the XMLNode, Kindly help is probably my first experience with LINQ as well as XML.
XNamespace rs = "urn:schemas-microsoft-com:rowset";
XNamespace z = "#RowsetSchema";
XDocument doc = XDocument.Load(...);
var result = doc.Element(rs + "data")
.Elements(z + "row")
.Select(e => (string)e.Attribute("ows_Country"))
.Distinct()
.ToList();

Categories

Resources