XML parsing with C #

I have an XML file as follows:
Xml file

I downloaded the XML file: http://dl.dropbox.com/u/10773282/2011/result.xml . This is an XML generated machine, so you may need an XML editor / editor.

I use this C # code to get elements in CoverageDSPriv/Module/*.

using System;
using System.Xml;
using System.Xml.Linq;

namespace HIR {
  class Dummy {

    static void Main(String[] argv) {

      XDocument doc = XDocument.Load("result.xml");

      var coveragePriv = doc.Descendants("CoverageDSPriv"); //.First();
      var cons = coveragePriv.Elements("Module");

      foreach (var con in cons)
      {
        var id = con.Value;
        Console.WriteLine(id);
      }
    }
  }
}

By running the code, I get this result.

hello.exe6144008016161810hello.exehello.exehello.exe81061hello.exehello.exe!17main_main40030170170010180180011190190012200200013hello.exe!107testfunctiontestfunction(int)40131505001460600158080216120120017140140018AA

I expect to receive

hello.exe
61440
...

However, I get only one line of a long line.

  • Q1: What could be wrong?
  • Q2: How to get # elements in the cons? I tried cons.Count, but it does not work.
  • Q3: If I need to get a nested value <CoverageDSPriv><Module><ModuleNmae>, I use this code:

    var coveragePriv = doc. ( "CoverageDSPriv" );//.(); var cons = coveragePriv.Elements( "Module" ). ( "ModuleName" );

, , , , . ?

ADDED

var cons = coveragePriv.Elements("Module").Elements();

, NamespaceTable .

hello.exe
61440
0
8
0
1
6
1
61810hello.exehello.exehello.exe81061hello.exehello.exe!17main_main40030170170010180180011190190012200200013hello.exe!107testfunctiontestfunction(int)40131505001460600158080216120120017140140018

Linq to XML , .

+3
2

, Module - .Value InnerText . ?

coveragePriv.Element("Module").Elements();

Module, , , .

Update:

<NamespaceTable> <Module>, , , <Module>, . , <NamespaceTable>:

foreach (var con in cons)
{
    if (con.Name == "NamespaceTable") 
    {
        foreach (var nsElement in con.Elements()) 
        {
            var nsId = nsElement.Value;
            Console.WriteLine(nsId);
        }
    }
    else
    {
        var id = con.Value;
        Console.WriteLine(id);
    }
}

, , .Descendents():

var cons = coveragePriv.Element("Module").Descendents();

foreach (var con in cons)
{
    var id = con.Value;
    Console.WriteLine(id);
}
+4

XMLElement.Value . XML .net xml. , , , .

xml, , XML , .

1) XSLT, , xml html. . , - html.

2) XML. # , , # . MS serlization XML. , , , . - XML , , .

3) Linq XML. XML, . , , .

+1

Source: https://habr.com/ru/post/1792294/


All Articles