1

My xml document is as follows (but much larger and not anything to do with date of birth info). It is produced by a method and I want to access information from it.

I would like to look up a "Name" like John or Hannah and get DOB from the next line and perhaps put them into a Dictionary<string,string> for later look-up.

The document is ALWAYS in this form and Names are unique. I only need the first section name = "DOBs".

<info>
  <section name="DOBs">
   <table>
    <headers>
      <cell value="Name" />
      <cell value="DOB" />
    </headers>
    <row>
      <cell value="John" />
      <cell value="121245" />
    </row>
    <row>
      <cell value="Hannah" />
      <cell value="050595" />
    </row>
   </table>
  </section>
  <section name="notDOBs">
   <table>
    <headers>
      <cell value="Name" />
      <cell value="other info" />
    </headers>
    <row>
      <cell value="Hannah" />
      <cell value="blue" />
    </row>
    ....
    </table>
  </section>
</info>

What have I tried? - to be honest, not a lot as I'm confused about xml in general. I've looked at a few SO entries: Xml Reader,This one and How to use XMLreader to read this xml. I also looked at csharp-examples.net which looked good but I kept getting errors about "not leading to node set".

I got excited when I found this: get xml attribut values but no joy, it doesn't seem to find them only the ones.

var myXml = XElement.Load(_myPath).Elements();
var myArray = myXml.Elements("cell").Attributes("value").Select(n => n.Value);

I know I have a LOT more work to do but sometimes you just have to bite the bullet and ask ... Can anyone help?

Community
  • 1
  • 1
Sisyphus
  • 679
  • 6
  • 21

2 Answers2

1
var list = XDocument.Load("C:\\test.xml")
            .Descendants("section")
            .Where(e => e.Attribute("name").Value.Equals("DOBs"))
            .Descendants("row")
            .Descendants("cell")
            .Attributes("value").ToList();

var dic = Enumerable.Range(0, list.Count/2)
    .ToDictionary(i => list[2*i], i => list[2*i + 1]);
cuongle
  • 74,024
  • 28
  • 151
  • 206
  • can I just ask how that would change if I had another section that I didn't care too much about called '
    ...
    ' (sorry, I forgot about that - it only showed up when your piece of code worked so well!)
    – Sisyphus Oct 18 '12 at 05:25
  • @Sisyphus: what do you mean, could you update your question on this? – cuongle Oct 18 '12 at 05:26
  • Thanks @Cuong - have made the edit to include the second
    part that I don't need.
    – Sisyphus Oct 18 '12 at 05:31
0

Sort XML nodes alphabetically on attribute name

Check the above sample

in navigator try to use

      .MoveFirstChild() method
      .MoveNext() method
      .MoveToFirstAttribute() method
      .MoveToNextAttribute() method
      .Name property
      .Value property

do not forget to check if the current one is null or not

Community
  • 1
  • 1
Ozgur
  • 258
  • 3
  • 13
  • Thanks for the link, it gave me this ... 'myXml.Descendants("cell")' ... which I think may help dig down into the xml docuemnt - I was looking at the top level I think. – Sisyphus Oct 18 '12 at 05:03