C# Querying an XML Document

2019-08-21 10:57发布

Good Day,

I am trying to query an XML document and have the following query:

XElement root = XElement.Load(@"..\..\Data.xml");
var entries = root.Descendants()
              .Where(x => x.Name.LocalName == "Entry")
              .ToList();

Console.WriteLine("There are {0} nodes...", entries.Count());
foreach (XElement v in entries)
{
    Console.WriteLine(v.Value);
}

and this code works because it pulls the correct number of Entry nodes. The Entry nodes look like:

<?xml version="1.0" encoding="UTF-8"?>
<Database xmlns="http://www.someurl.org/schemas">
    <InfoFromRecord>
        <BaseData>
            <Entry>
                <Date>2006-03-08</Date>XD
                <Time>09:20:00</Time>
                <EnteredBy>DNS</EnteredBy>
                <TextEntry>Record 1</TextEntry>
            </Entry>
            <Entry>
                <Date>2006-03-08</Date>
                <Time>09:33:00</Time>
                <EnteredBy>MW</EnteredBy>
                <TextEntry>Record 2</TextEntry>
            </Entry>
            <Entry>
                <Date>2006-03-08</Date>
                <Time>08:58:00</Time>
                <EnteredBy>BH</EnteredBy>
                <TextEntry>Record 3</TextEntry>
            </Entry>
        </BaseData>
    </InfoFromRecord>
</Database>

The problem is, I want to extract only the Date and Time, not all four fields.

4条回答
地球回转人心会变
2楼-- · 2019-08-21 11:29
foreach (XElement v in entries)
{
    Console.WriteLine(v.Element("Date").Value);
    Console.WriteLine(v.Element("Time").Value);
}

Do not forget that Descendants finds children at any level, i.e. children, grand-children, etc where Elements find only direct child. So i guess Elements is the safe option in most of the cases.

EDIT : After seeing the XML

You need to include the Namspace also when getting the data

XNamespace ns = "http://www.someurl.org/schemas";
var entries = elm.Descendants().Where(x => x.Name.LocalName == "Entry").ToList();
foreach (XElement v in entries)
{
    Console.WriteLine(v.Element(ns+"Date").Value);
    Console.WriteLine(v.Element(ns+"Time").Value);
}
查看更多
对你真心纯属浪费
3楼-- · 2019-08-21 11:36

Let's assume your entire XML file looks like this for a clear example:

<Entries>
    <Entry>
        <Date>2006-03-08</Date>
        <Time>09:33:00</Time>
        <EnteredBy>XX</EnteredBy>
        <TextEntry>Test Data</TextEntry>
    </Entry>
</Entries>

You could then do something like this:

var document = XDocument.Load(@"..\..\Data.xml");
var dateAndTimes =
    from d in document.Root.Descendants("Entry")
    select new
                {
                    Date = d.Element("Date").Value,
                    Time = d.Element("Time").Value
                };

From there, the dateAndTimes type will select an anonymous type of the Date and Time. You can change the anonymous type to be your own type, or something else.


EDIT: The problem is your xmlns. Change your code like so:

XNamespace namespc = "http://www.someurl.org/schemas";
var document = XDocument.Parse(xml);
var dateAndTimes =
    from d in document.Root.Descendants(namespc + "Entry")
    select new
                {
                    Date = d.Element(namespc + "Date").Value,
                    Time = d.Element(namespc + "Time").Value
                };
查看更多
乱世女痞
4楼-- · 2019-08-21 11:45
  IEnumerable<XElement> de = from el in xdoc.Descendants() select el;
  foreach (XElement el in de)
    {
     if (string.Equals(el.Name.ToString(), "movie", 
          `StringComparison.InvariantCultureIgnoreCase))`enter code here`             
               StringComparison.InvariantCultureIgnoreCase))
                    {
                        date(el);
                    }
查看更多
不美不萌又怎样
5楼-- · 2019-08-21 11:53

I haven't had a chance to try it but something like the following may give you what you are looking for

var entries = from i in root.Descendants() 
where Name=='entry' 
let date = i.Element('Date').Value 
let time = i.ELement('Time').Value 
select new Tuple<string,string>(date,time);
查看更多
登录 后发表回答