getting the attribute values of XML node - c#

i am trying to get the values of the Arg1,2 and 3 attributes of the below XML...in the XML, there are 2 different values and the iteration through the code has been verified to iterate twice. but the same answer is displayed twice and i don't know what is missing...!!!
this is the XML:
-<event type="2VO">
-<properties>
<schedule endOffset="00:00:22:00" endType="Duration" startOffset="00:00:33:00" startType="-ParentEnd"/>
<event title="Pixel VO" reconcileKey="106251137"/>
+<mediaStream>
<media Arg8="" Arg7="" Arg6="" Arg5="" Arg4="" Arg3="O1T13810" Arg2="1910" Arg1="TON" RuleCode="2VO"/>
</properties>
</event>
-<event type="2VO">
-<properties>
<schedule endOffset="00:00:22:00" endType="Duration" startOffset="00:00:33:00" startType="-ParentEnd"/>
<event title="Pixel VO" reconcileKey="106251137"/>
+<mediaStream>
<media Arg8="" Arg7="" Arg6="" Arg5="" Arg4="" Arg3="O1T13810" Arg2="1932" Arg1="TUE" RuleCode="2VO"/>
</properties>
</event>
and the code is below here:
static void Main(string[] args)
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(#"C:\Users\namokhtar\Desktop\testxml.xml");
foreach (XmlNode node in xdoc.SelectNodes("//event[#type='2VO']")) //or /CATALOG/CD
{
var x = node.SelectSingleNode("//#Arg1").Value;
var y = node.SelectSingleNode("//#Arg2").Value;
var z = node.SelectSingleNode("//#Arg3").Value;
Console.WriteLine("The first parameter is: " + x);
Console.WriteLine("The first parameter is: " + y);
Console.WriteLine("The first parameter is: " + z);
Console.ReadKey();
}

The problem is that the "//" in "//#Arg1" is telling it to look anywhere in the document not specifically the node you have selected so even thought you extract each <event> node, you then always get the first "//#Arg1" attribute value in the document.
Either use ".//#Arg1" to tell it to search relative to the current node or be more specific with your XPath and use "./properties/media/#Arg1"
If it's any consolation, I don't find this "//" behaviour intuitive!

Related

How to access Data from XML File in C#

how can I access data coming from an XML File into variables?
<RESULTS>
<ROW>
<COLUMN NAME="OM_USER"><![CDATA[TEXT]]></COLUMN>
<COLUMN NAME="LASTNAME"><![CDATA[TEXT]]></COLUMN>
<COLUMN NAME="FIRSTNAME"><![CDATA[TEXT]]></COLUMN>
<COLUMN NAME="PHONE"><![CDATA[TEXT]]></COLUMN>
</ROW>
</RESULTS>
this is what i tried but the output is weired
XDocument xmlDocument = XDocument.Load(#"PATH");
var xElement = xmlDocument.Element();
if (xElement != null)
{
foreach (var child in xElement.Elements())
{
Console.WriteLine(child.Name + ": " + child.Value);
}
}
My Output come like this:
ROW: TEXTEXTTEXTTEXT
ROW: TEXTEXTTEXTTEXT
ROW: TEXTEXTTEXTTEXT
ROW: TEXTEXTTEXTTEXT
Thanks for any help =)
The problem that you are facing is that you are a level too high in the xml tree. By stating
var xElement = xmlDocument.Elements();
You get all the elements from the top of the xdocument, which is the root named "RESULTS"
Your foreach is looping over all the child's of the "RESULTS" element which is only "ROW". If you the specify that you want to print the name of the child (ROW) and its value you get all the text that element "ROW" and all it's children have. If you use the debugger you will see this content.
By changing your code to something simular below you will get the correct outcome
const string msg = "<RESULTS><ROW><COLUMN NAME=\"OM_USER\"><![CDATA[TEXT]]></COLUMN><COLUMN NAME=\"LASTNAME\"><![CDATA[TEXT]]></COLUMN><COLUMN NAME=\"FIRSTNAME\"><![CDATA[TEXT]]></COLUMN><COLUMN NAME=\"PHONE\"><![CDATA[TEXT]]></COLUMN></ROW></RESULTS>";
XDocument xmlDocument = XDocument.Parse(msg);
//get the first element named ROW from the xdocument.
var xElement = xmlDocument.Descendants().First(x => x.Name == "ROW");
if (xElement != null)
{
//Loop through the childs of ROW
foreach (var child in xElement.Elements())
{
//Find the first attribute in the child element which is named NAME.
var childAttrName = child.Attributes().First(x => x.Name == "NAME");
//Print the value of the attribute called name and the value of the element.
System.Console.WriteLine(childAttrName.Value + " : " + child.Value);
}
}

Xelement adds element value to itself twice

In my code i iterate through an xelement and have it return the value of each node within that element e.g.
foreach(XElement n in XDocument.Descedants("element_name)
{
Console.WriteLine("Searching: " n.Value);
}
My problem is the both <Directory> elements are returned in the string
Searching: C:\Users\215358\OneDrive\MusicC:\Users\215358\Dropbox\Music
My XML file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<Directories>
<Directory>C:\Users\215358\OneDrive\Music</Directory>
<Directory>C:\Users\215358\Dropbox\Music</Directory>
</Directories>
I expect it to output the second line element in <Directory> like this:
C:\Users\215358\Dropbox\Music
Why is this happening?
XElement.Value gets the concatenated text contents of an element. This includes the text of child elements which is not always very helpful. If you just want the text from the current element, you can find the text node in its child nodes.
foreach(XElement n in XDocument.Descedants("Directory"))
{
var text = n.Nodes().Where (x => x is XText).Cast<XText>().FirstOrDefault ();
if(text!=null){
Console.WriteLine("Searching: " + text.Value);
}else{
Console.WriteLine("No text node found");
}
}
Since you want to iterate through each entry and search element value, you could do something like this.
foreach (var element in doc.Descendants("Directory"))
{
if((string)element.Value == "searchstring")
{
// your logic
}
}
In case if you are looking for second element in the xml, you could apply Skip extension to skip specified count of elements.
var secondelement = doc.Descendants("Directory").Skip(1); // Skip first element;
or if you are looking for last element, you could take Last or LastOrDefault extension.
var lastelement = doc.Descendants("Directory").LastOrDefault();
Check this example.

Iterating through XML file with XDocument returns element with no attributes

I'm iterating through all the child elements of this XML file:
<?xml version="1.0" encoding="utf-8"?>
<users>
<user name="SemiViral" access="2" />
</users>
with this code:
XDocument doc = XDocument.Load("Users.xml");
Console.WriteLine(doc.Descendants("users").Count());
foreach (XElement u in doc.Descendants("users")) {
Console.WriteLine(u.Attributes().Count());
}
but the output from the WriteLine is 0, and similarly empty if I try referencing the attributes directly. Counting the descendants returns 1 and when I added inner contents to the single child element, it was able to output those. So I know that it's the correct element, it's simply not accessing the attributes for some reason.
Here is a code to do what you are trying to do. You were not getting results because you were only looking for users elements (doc.Descendants("users")). The element that you are looking for is at the next level of the xml. If you debugged your code you would have spotted it.
XDocument doc = XDocument.Load("Users.xml");
Console.WriteLine(doc.Descendants("users").Descendants().Count());
foreach (XElement u in doc.Descendants("users").Descendants())
{
Console.WriteLine("value of the attribute is " + u.Attributes("access").First().Value);
}

Xml delete node by elements value

Here is xml file:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Data>
<PageInfo>
<ID>0</ID>
<NUM>5</NUM>
<URL>er.php</URL>
</PageInfo>
<PageInfo>
<ID>1</ID>
<NUM> 12345</NUM>
<URL>/out/out.ViewFolder.php</URL>
</PageInfo>
</Data>
I have tried alot of ways (for a week now) to delete certain node (PageInfo) by element (ID,NUM,URL) in this xml file.
There are few approaches I have tried:
1st approach:
XmlDocument docc = new XmlDocument();
docc.LoadXml(AppDomain.CurrentDomain.BaseDirectory + "/WebData.xml");
XmlNode nodee = docc.SelectSingleNode("/Data/PageInfo/ID[2]");
nodee.RemoveAll();
2nd approach:
XmlDocument document = new XmlDocument();
document.Load(AppDomain.CurrentDomain.BaseDirectory + "/WebData.xml");
XmlNodeList nodes = document.DocumentElement.SelectNodes("/Data/PageInfo");
string ID, NUM, URL;
foreach (XmlNode node in nodes)
{
ID = node.SelectSingleNode("ID").InnerText;
NUM = node.SelectSingleNode("NUM").InnerText;
URL = node.SelectSingleNode("URL").InnerText;
node.RemoveAll();
Console.WriteLine(ID + " " + NUM + " " + URL + "\n");
}
1st solution does not trigger and exception but nothing happens, 2nd solution throws an exception: Data at the root level is invalid.
How one would be able to delete nodes by elements value in an xml file? (LINQ is fine)
Disclaimer: all solutions I have found on StackOverflow does not work for my certain case.
Based on the ID, please try this solution :
First approach
string xml = AppDomain.CurrentDomain.BaseDirectory + "/WebData.xml";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xml);
XmlNode t = xmlDoc.SelectSingleNode("/Data/PageInfo[ID='0']");
t.ParentNode.RemoveChild(t);
xmlDoc.Save(xml);
Second approach : Linq
XDocument xmlDoc = XDocument.Load(xml);
var pageInfo = (from xml2 in xmlDoc.Descendants("PageInfo")
where xml2.Element("ID").Value == "0"
|| xml2.Element("NUM").Value == "5"
|| xml2.Element("URL").Value == "er.php"
select xml2).FirstOrDefault();
pageInfo.Remove();
xmlDoc.Save(xml);
// output
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Data>
<PageInfo>
<ID>1</ID>
<NUM> 12345</NUM>
<URL>/out/out.ViewFolder.php</URL>
</PageInfo>
</Data>
As <Data> is an array so you can deserialize it in a classData which has List<PageInfo> so you can update your data accordingly and then serialize it back in your file.
Example:
XmlArray("Data")]
public class Data
{
[XmlArrayItem("PageInfo")]
public List<PageInfo> pageInfos = new List<PageInfo>();
}
public class PageInfo
{
public int ID;
public int NUM;
public string URL;
}
Now you can apply queries on your list and then deserialize your Data class back to file. See This Link for Serializing guide.

XML Xpath expression

I am trying to get the elements title and runtime (siblings) where the runtime value is larger than the input value. My C# code with the XPath expression is:
ElementValue = 140;
nodeList = root.SelectNodes(#"/moviedb/movie[./runtime>'" + ElementValue + "'/title | /moviedb/movie[./runtime>'" + ElementValue + "']/runtime");
This XPath expression is not returning anything.
My XML file:
<moviedb>
<movie>
<imdbid>tt0120689</imdbid>
<genres>Crime,Drama,Fantasy,Mystery</genres>
<languages>English,French</languages>
<country>USA</country>
<rating>8.5</rating>
<runtime>189</runtime>
<title lang="english">The Green Mile</title>
<year>1999</year>
</movie>
<movie>
<imdbid>tt0415800</imdbid>
<genres>Action,Animation,Drama,Thriller</genres>
<languages>English</languages>
<country>USA</country>
<rating>4.5</rating>
<runtime>139</runtime>
<title lang="english">Fight Club</title>
<year>2004</year>
</movie>
</moviedb>
You can instead use linq2xml
var doc=XDocument.Load(path);
var movies=doc.Elements("movie")
.Where(x=>(int)x.Element("runtime")>input)
.Select(x=>new
{
Title=x.Element("title").Value,
Runtime=(int)x.Element("runtime")
});
You can now iterate over movies
foreach(var movie in movies)
{
movie.Title;
movie.Runtime;
}
You seem to be applying the values you want off the node as a filter criteria, which won't work. I would go about this another way, first finding the nodes which meet the criteria:
nodeList = root.SelectNodes(#"/moviedb/movie[runtime > " + ElementValue + "]");
And then grabbing the child elements from each:
foreach (var node in nodeList)
{
Debug.WriteLine(node.SelectSingleNode("title").InnerText);
Debug.WriteLine(node.SelectSingleNode("runtime").InnerText);
}
You can do this using a single XPath expression by performing a union i.e. the | operator. As mentioned in other answers here, you had your select inside your predicate which would not result in the correct answer for you anyway.
Note, if you want to see if a number is bigger than another number, unless you are using a Schema driven data-type aware XQuery engine you will need to cast the text() to a number before performing the comparison. In this instance I have assumed an xs:int will be suitable for you. Also you can use the atomic gt as opposed to = which may be more efficient.
ElementValue = 140;
nodeList = root.SelectNodes(#"/moviedb/movie[xs:int(runtime) gt " + ElementValue + "]/(title | runtime)");

Categories