Huzaifa
Huzaifa

Reputation: 1171

How to print each element with root attribute of the node of an XML file in c#

I am trying to print each element(Reference) of root node "Identity" with the attribute value of "Identity".

For example:

I want to print

00002C8DGPDDTZ5D, S2513.268898, John S2613.269022, JOHN 

in one line then for

000388MRUO7TRSBH, S2513.285402,Doe, S2613.285476Doe

in another and so on.

Please let me know if I am not making myself clear.

Thanks

The XML file

<Identity Identifier="00002C8DGPDDTZ5D" CDate="2013-02-07">
<References>
    <Reference>
        <Value>A^S2513.268898|B^JOHN</Value>
        <Traces/>
    </Reference>
    <Reference>
        <Value>A^S2613.269022|B^JOHN</Value>
        <Traces/>
    </Reference>
</References>
 <Identity Identifier="000388MRUO7TRSBH" CDate="2013-02-07">
<References>
    <Reference>
        <Value>A^S2513.285402|B^Doe</Value>
        <Traces/>
    </Reference>
    <Reference>
        <Value>A^S2613.285476|B^Doe</Value>
        <Traces/>
    </Reference>
</References>

Also,

I tried the following code.

 XmlNodeList OysterID = doc.GetElementsByTagName("Identity");  
     XmlNodeList Reference = doc.GetElementsByTagName("Value");
 for (int j = 1; j <= i; j++)

                    s[j] = Reference[j].InnerText;

I want something similar. I want a for loop which will print each element of a root with the root attribute value. I am not able to think how I can get the root attribute value with each element.

Upvotes: 0

Views: 164

Answers (2)

Evelie
Evelie

Reputation: 3069

How about something like this

        foreach (XmlNode element in xmlDocument.GetElementsByTagName("Identity"))
        {
            string output = element.Attributes[0].Value;
            foreach (XmlNode xmlNode in element.ChildNodes)
            {
                foreach (XmlNode reference in xmlNode.ChildNodes)
                {
                    output += reference.InnerText;
                }
            }
            //Output here should be onelined.. 
        }

The output will not be formatted exactly as you wanted, so some formatting is needed. But you will have it on one line :)

Upvotes: 1

SixOThree
SixOThree

Reputation: 771

Try this.

        string xml = @"<Identity Identifier=""00002C8DGPDDTZ5D"" CDate=""2013-02-07"">      <References>        <Reference>             <Value>A^S2513.268898|B^JOHN</Value>            <Traces/>       </Reference>        <Reference>             <Value>A^S2613.269022|B^JOHN</Value>            <Traces/>       </Reference>    </References>   <Identity Identifier=""000388MRUO7TRSBH"" CDate=""2013-02-07"">     </Identity>     <References>        <Reference>             <Value>A^S2513.285402|B^Doe</Value>             <Traces/>       </Reference>        <Reference>             <Value>A^S2613.285476|B^Doe</Value>             <Traces/>       </Reference>    </References>  </Identity>    ";
        XDocument xmlDoc = XDocument.Parse(xml);
        IEnumerable<XElement> entries = xmlDoc.Descendants("Reference");
        foreach (XElement entry in entries) {
            string temp = entry.Value;
        }

Upvotes: 0

Related Questions