Getting XML node based on text value, that ends with XML - c#

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"));

Related

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);

Looping through xmlnodes using Xdocument

I have an xml string like this
<Test>
<ConnectionParameters>
<ConnectionParameter DisplayName="asd" Id="cgfh" IsPassword="false" IsRequired="true"> </ConnectionParameter>
<ConnectionParameter DisplayName="asdasd" Id="fgh" IsPassword="false" IsRequired="true"></ConnectionParameter>
<ConnectionParameter DisplayName="asdasd" Id="hdfh" IsPassword="false" IsRequired="true"></ConnectionParameter>
<ConnectionParameter DisplayName="asdad" Id="dfgdf" IsPassword="false" IsRequired="true"> </ConnectionParameter>
</ConnectionParameters>
</Test>
How can I loop through each "ConnectionParameter" tag inorder to get the attributes like Id,DisplayName etc using xdocument?
I tried like this,
XDocument xdoc = new XDocument();
xdoc= XDocument.Parse(fileContent);
var saveResult = from b in xdoc.Descendants("ConnectionParameters")
select new
{
success = (string)b.Element("ConnectionParameter").Attribute("Id").Value ?? string.Empty,
};
But it only returns the first node only
You're currently looping through all the ConnectionParameters elements (of which there's only one) and selecting the first ConnectionParameter element (using the Element call). You want to just loop through the ConnectionParameter elements:
// Note the lack of creating a new XDocument for no reason
XDocument xdoc = XDocument.Parse(fileContent);
var saveResult = from b in xdoc.Descendants("ConnectionParameter")
select new
{
success = (string) b.Attribute("Id") ?? ""
};
Or to avoid creating an anonymous type for no obvious reason, and using plain "dot notation" as the query expression isn't helping you much:
XDocument xdoc = XDocument.Parse(fileContent);
var saveResult = xdoc.Descendants("ConnectionParameter")
.Select(b => (string) b.Attribute("Id") ?? "");
If you prefer to make the parent element names explicit, you could use:
XDocument xdoc = XDocument.Parse(fileContent);
var saveResult = xdoc.Element("Test")
.Element("ConnectionParameters")
.Descendants("ConnectionParameter")
.Select(b => (string) b.Attribute("Id") ?? "");
Or:
XDocument xdoc = XDocument.Parse(fileContent);
var saveResult = xdoc.Root
.Element("ConnectionParameters")
.Descendants("ConnectionParameter")
.Select(b => (string) b.Attribute("Id") ?? "");
Load your xml into xDocument, then you can do something like this (can't remember exact syntax)
xDoc.Root.Descendants("ConnectionParameters").Attribute("DisplayName").Value;

Get node property of XmlDocument in C#

I'm using this xml structure:
<park>
<car title="Ferrari" available="true">
<url>http://www.ferrari.com/</url>
</rss>
</park>
And this is my code in C#:
XmlDocument doc = new XmlDocument();
doc.Load("Settings.xml");
XmlNodeList list = doc.SelectNodes("/park/car");
foreach (XmlNode item in list)
{
string x = item["#title"].InnerText;
}
I just want to get "title" property but i can't get it working. I'm using "#" but without success.
Try this code:
string x = item.Attributes["title"].Value;
I suggest you to use LINQ to XML for parsing xml:
var xdoc = XDocument.Load("Settings.xml");
var titles = xdoc.XPathSelectElements("//park/car")
.Select(c => (string)c.Attribute("title"));
Or without XPath:
var titles = xdoc.Descendants("park")
.Elements("car")
.Select(c => (string)c.Attribute("title"));

How to Select XML node, given only the attribute name

I have an XML file as follow
<NODE1 attribute1 = "SomeValue" attribute2 = "SomeOtherValue" />
<NODE2 attribute3 = "SomeValue" attribute4 = "SomeOtherValue" />
Now I am given only the attribute name say "attribute3". How can I get the name of node?
Add the following namespace at the top of your file:
using System.Xml.Linq;
And try this (assuming that input.xml is the path to your XML file):
var xml = XDocument.Load("input.xml");
string nodeName;
var node = xml.Descendants()
.FirstOrDefault(e => e.Attribute("attribute3") != null);
if (node != null)
nodeName = node.Name.LocalName;
With LINQ to XML:
XDocument xdoc = XDocument.Load(path_to_xml);
var nodes = xdoc.Descendants().Where(e => e.Attribute("attribute3") != null);
Or with XPath (as Marvin suggested):
var nodes = xdoc.XPathSelectElements("//*[#attribute3]");
Both queries will return collection of XElement nodes which have attribute attribute3 defined. You can get first of them with FirstOrDefault. If you want to get just name, use node.Name.LocalName.
UPDATE: I do not recommend you to use XmlDocument, but if you already manipulating this xml document, then loading it second time with XDocument could be inefficient. So, you can select nodes with XPathNavigator:
var doc = new XmlDocument();
doc.Load(path_to_xml);
var naviagator = doc.CreateNavigator();
var nodeIterator = naviagator.Select("//*[#attribute3]");
try in this way
string nodeName;
if(Node.Attributes.Cast<XmlAttribute>().Any(x => x.Name == "attribute3"))
{
nodeName=Node.Name;
}

remove sections of XML document with Linq

How would i using Linq remove all section where their element contains parameter with {} ? In my example i want to remove section with {SecName1}
Source document:
<ReceiptLayoutMaintenanceRequest>
<ReceiptLayoutName>Test Layout1</ReceiptLayoutName>
<ActionName>Add</ActionName>
<ReceiptLayoutForMaintenance>
<Name>Test Layout1</Name>
<Description>ReciptDesc</Description>
<PrinterName>Emulator - Receipt</PrinterName>
<ReceiptLayout>
<Name>AAA</Name>
<Description>$</Description>
<TemplateName>DefaultTemplate</TemplateName>
<LayoutParameters />
</ReceiptLayout>
<ReceiptLayout>
<Name>{SecName1}</Name>
<Description>$</Description>
<TemplateName>DefaultTemplate</TemplateName>
<LayoutParameters />
</ReceiptLayout>
</ReceiptLayoutForMaintenance>
</ReceiptLayoutMaintenanceRequest>
Wanted output
<ReceiptLayoutMaintenanceRequest>
<ReceiptLayoutName>Test Layout1</ReceiptLayoutName>
<ActionName>Add</ActionName>
<ReceiptLayoutForMaintenance>
<Name>AAA</Name>
<Description>ReciptDesc</Description>
<PrinterName>Emulator - Receipt</PrinterName>
<ReceiptLayout>
<Name>AAA</Name>
<Description>$</Description>
<TemplateName>DefaultTemplate</TemplateName>
<LayoutParameters />
</ReceiptLayout>
</ReceiptLayoutForMaintenance>
thanks
This removes any ReceiptLayout node which has a child Name that starts and ends with brackets and produces your desired output:
XDocument doc = XDocument.Load(#"test.xml"); //load xml
var nodesToRemove = doc.Descendants("ReceiptLayout")
.Where(x => x.Element("Name").Value.StartsWith("{")
&& x.Element("Name").Value.EndsWith("}"))
.ToList();
foreach (var node in nodesToRemove)
node.Remove();
This can be shortened into one Linq statement, personally I prefer to keep Linq query and modification (removal) separate though:
doc.Descendants("ReceiptLayout")
.Where(x => x.Element("Name").Value.StartsWith("{")
&& x.Element("Name").Value.EndsWith("}"))
.Remove();
var doc = XDocument.Parse(xml);
doc.Descendants()
.Where(n => !n.HasElements && Regex.IsMatch(n.Value, "[{].*?[}]"))
.Select(n=>n.Parent) // because you want to remove the section not the node
.Remove();
xml = doc.ToString();
A variant of BrokenGlass code using the let keyword
var doc = XDocument.Load(#"test.xml");
var list = from p in doc.Descendants("ReceiptLayout")
let q = p.Element("Name")
let r = q != null ? q.Value : string.Empty
where r.StartsWith("{") && r.EndsWith("}")
select p;
list.Remove();
This is "premature optimization" :-) I "cache" the p.Element("Name").Value. Ah... And I check if really there is a Name Element so that everything doesn't crash if there isn't one :-)

Categories

Resources