-4

Sorry for the simplicity, but this one is eluding me , here is the xml

<tarification cle="i54534452">
    <gamme reference="new-securite-sante">
        <tarif formule="NS1">26.12</tarif>
        <tarif formule="NS2">29.08</tarif>
    </gamme>
    <gamme reference="new-equilibre-sante">
        <tarif formule="125">32.92</tarif>
        <tarif formule="150">42.20</tarif>
        <tarif formule="200">52.04</tarif>
    </gamme>
    <gamme reference="new-full-sante">
        <tarif formule="125">36.81</tarif>
        <tarif formule="150">43.86</tarif>
        <tarif formule="200">52.07</tarif>
    </gamme>
    <gamme reference="new-performance">
        <tarif formule="125">40.47</tarif>
        <tarif formule="150">49.18</tarif>
        <tarif formule="200">55.91</tarif>
    </gamme>
</tarification>

and here is the code C#

XDocument docc = XDocument.Parse(chaineXML);

var formule = docc.Descendants("tarif").Attributes("formule")
    .Select(x => x.Value).ToList();
var tarif = docc.Descendants("tarif")
    .Select(x => x.Value).ToList();

How i can get the attribu formule and all elements tarif ??? It is showing an exception "Data at the root issue : level is invalid".

  • 6
    The XML looks correctly formatted.. can you see what is the value of chaineXML variable before you do XDocument.Parse? – azeem Dec 30 '13 at 14:59
  • 2
    Is this the entire XML? Make this an SSCCE by including the exact initialization of chaineXML. – H H Dec 30 '13 at 15:05
  • Is there more than one ``tarification`` element in ``chaineXML`` string? I am thinking that is the case because the element has a key attribute. You can't use XDocument.Parse to parse a string containing multiple root level elements. – Boluc Papuccuoglu Dec 30 '13 at 15:06
  • The above code shows something else on my machine .. "Data at the root level is invalid. Line 1, position 1." – Pradip Dec 30 '13 at 15:07
  • Hey!!.. Mycode works now. Not sure about the problem. – Pradip Dec 30 '13 at 15:10
  • that is the entire XML – user3030806 Dec 30 '13 at 15:14
  • 2
    I copied your code into a unit test and it worked on the first try. – Chuck Conway Dec 30 '13 at 16:45
  • Maybe something to do with this?: http://stackoverflow.com/questions/291455/xml-data-at-root-level-is-invalid?rq=1 – Chris Dec 30 '13 at 17:15

2 Answers2

2

Try this one with Elements:

XDocument myList = XDocument.Load(@"E:\a.xml");
var obj = myList
    .Elements("tarification")
    .Descendants("tarif")
    .Attributes("formule")
    .Select(s => s.Value)
    .ToList();

foreach (var item in obj)
{
    Console.WriteLine(item);
}
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Gun
  • 1,400
  • 1
  • 10
  • 16
0

I am thinking that there is more than one tarification element in chaineXML string because the element has a key attribute, and that is the problem you are experiencing. You can't use XDocument.Parse to parse a string containing multiple root level elements

Does your code work when you change the first line of your code to:

 XDocument docc = XDocument.Parse("<ROOTELEMENT>"+chaineXML+"</ROOTELEMENT>");

What this does is effectively wrap all tarification elements into a root document element.

Boluc Papuccuoglu
  • 2,318
  • 1
  • 14
  • 24