Querying XML document using Linq
Replace your LINQ where
query with this one -
IEnumerable<XElement> rows = xDoc.Descendants().Where(d => d.Name == "Person"
&& d.Descendants().Any(e => e.Name == "Course"
&& e.Value == "GDICT-CN"));
Ramesh Sivaraman
using System.Text; using System.Collections.Generic; using System; public class MyDescription { private readonly byte[] Intro = new byte[]{83,111,109,101,111,110,101,32,118,101,114,121,32,112,97,115,115,105,111,110,97,116,101,32,97,98,111,117,116,32,116,101,99,104,110,111,108,111,103,121,46,46,46}; private readonly byte[] Description = new byte[]{73,32,101,110,106,111,121,32,99,111,100,105,110,103,32,97,110,100,32,73,32,108,111,118,101,32,116,111,32,109,101,101,116,32,112,101,111,112,108,101,32,119,104,111,32,99,97,110,32,100,105,115,99,117,115,115,32,116,104,105,110,103,115,32,116,101,99,104,110,111,108,111,103,121,46,10,83,104,97,114,105,110,103,32,105,115,32,99,97,114,105,110,103,44,32,119,104,97,116,32,103,111,101,115,32,97,114,111,117,110,100,32,99,111,109,101,115,32,97,114,111,117,110,100,46,46,46,10,82,101,103,97,114,100,115,44,82,97,109,101,115,104}; private readonly byte[] ContactMe = new byte[]{114,97,97,109,48,51,48,64,108,105,118,101,46,99,111,109}; private Dictionary<String, byte[]> myStatements; private byte[] asciiString; public MyDescription() { myStatements = new Dictionary<String, byte[]>(); } private void WhoAmI() { myStatements.Add("Intro", Intro); myStatements.Add("Description", Description); myStatements.Add("ContactMe", ContactMe); } public void PrintMyDescription() { WhoAmI(); foreach(KeyValuePair<String, byte[]> myPair in myStatements) { Console.WriteLine("{0}: {1}", myPair.Key, ASCIIEncoding.ASCII.GetString(myPair.Value)); } } static void Main(string[] args) { MyDescription myDesc = new MyDescription(); myDesc.PrintMyDescription(); Console.ReadKey(); } }
Updated on November 12, 2020Comments
-
Ramesh Sivaraman over 3 years
I am trying to Linq an xml document, I am unable to query the inner elements as you can see from below code what I am trying to do. I want to get all records which has a certain name... Please help.
<?xml version="1.0" encoding="utf-8" ?> <Student> <Person name="John" city="Auckland" country="NZ" /> <Person> <Course>GDICT-CN</Course> <Level>7</Level> <Credit>120</Credit> <Date>129971035565221298</Date> </Person> <Person> <Course>GDICT-CN</Course> <Level>7</Level> <Credit>120</Credit> <Date>129971036040828501</Date> </Person> </Student>
And now the source
class Program { static void Main(string[] args) { string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); XDocument xDoc = XDocument.Load(path + "\\Student Data\\data.xml"); IEnumerable<XElement> rows = from row in xDoc.Descendants("Person") where (string)row.Attribute("Course") == "GDICT-CN" select row; foreach(XElement xEle in rows) { IEnumerable<XAttribute>attlist = from att in xEle.DescendantsAndSelf().Attributes() select att; foreach(XAttribute xatt in attlist) { Console.WriteLine(xatt); } foreach (XElement elemnt in xEle.Descendants()) { Console.WriteLine(elemnt.Value); } Console.WriteLine("-------------------------------------------"); } Console.ReadLine(); } }