0

I am newbie to LINQ. I am trying to read the following node and element values from XML using LINQ.

DATA -- msgid and msgtime PTP -- percentage CONT1 -- get "url" value if the "type" = "RIGHT"

Please let me know.

<DATA msgid="02123" msgtime="2008-02-29 15:30:02.123">
  <PTP number="67" pert="READ" percentage="95" pertime="2008-02-29 15:30:02.123">
    <Images>
      <Image view="w1" type="IMAGE" percentage="85" distance="0" url="00002_tyd.jpg" />
    </Images>
  </PTP>
  <CHAS1 sequence="1" number="58019" percentage="95" pertime="2008-02-29 15:30:02.123">
    <Images>
      <Image view="c1" type="WRONG" percentage="85" url="00002_ssj.jpg" />
      <Image view="c2" type="RIGHT" percentage="85" url="00003_ssj.jpg" />
    </Images>
    <CONT1 number="58011" percentage="95" pertime="2008-02-29 15:30:02.123">
      <Images>
        <Image view="c1" type="WRONG" percentage="85" url="00002_csj.jpg" />
        <Image view="c2" type="RIGHT" percentage="85" url="00003_csj.jpg" />
      </Images>
    </CONT1>
  </CHAS1>
</DATA>
5
  • <DATA msgid="02123" msgtime="2008-02-29 15:30:02.123"> <PTP number="67" pert="READ" percentage="95" pertime="2008-02-29 15:30:02.123"> <Images> </Images> </PTP> <CHAS1 sequence="1" number="58019" percentage="95" pertime="2008-02-29 15:30:02.123"> <CONT1 number="58011" percentage="95" pertime="2008-02-29 15:30:02.123"> <Images> <Image view="c1" type="WRONG" percentage="85" url="00002_csj.jpg" /> <Image view="c2" type="RIGHT" percentage="85" url="00003_csj.jpg" /> </Images> </CONT1> </CHAS1> </DATA> Commented Oct 8, 2009 at 19:04
  • Duplicate of stackoverflow.com/questions/1537831/… Commented Oct 8, 2009 at 19:04
  • @nav100 - Reformatted to reveal XML. You can use the code button (the 1010 button) to do this Commented Oct 8, 2009 at 19:05
  • @nav100 - are you wanting to read a specific node? Is that the entire file? Commented Oct 8, 2009 at 19:09
  • yes. I want to read a specific node. It's not the entire file. Commented Oct 8, 2009 at 19:12

1 Answer 1

1

I would suggest you take a look at this:

http://www.hookedonlinq.com/LINQtoXML5MinuteOverview.ashx

Particularly the part beginning with "Once these files are loaded into the LINQ to XML API, you can write queries over that tree."

Based on that, something like this should work for you:

XDocument loaded = XDocument.Load(@"C:\data.xml");
var q = from c in loaded.Descendants("DATA")
        select new 
        {
            MsgId = (int)c.Attribute("msgid"),
            MsgTime = (DateTime)c.Attribute("msgtime"),
            PtpPercentage = (int)c.Element("PTP").Attribute("percentage")
            ContUrls = from i in c.Element("CHAS1")
                                  .Element("CONT1")
                                  .Descendants("Image")
                       where (string)i.Attribute("type") == "RIGHT"
                       select (string)i.Attribute("url");
        };

Not tested, but that should put you on the right track.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.