Tito
Tito

Reputation: 37

getting the attribute values of XML node

i am trying to get the values of the Arg1,2 and 3 attributes of the below XML...in the XML, there are 2 different values and the iteration through the code has been verified to iterate twice. but the same answer is displayed twice and i don't know what is missing...!!!

this is the XML:

-<event type="2VO">
-<properties>

<schedule endOffset="00:00:22:00" endType="Duration" startOffset="00:00:33:00" startType="-ParentEnd"/>

<event title="Pixel VO" reconcileKey="106251137"/>

+<mediaStream>
<media Arg8="" Arg7="" Arg6="" Arg5="" Arg4="" Arg3="O1T13810" Arg2="1910" Arg1="TON" RuleCode="2VO"/>

</properties>

</event>

-<event type="2VO">
-<properties>

<schedule endOffset="00:00:22:00" endType="Duration" startOffset="00:00:33:00" startType="-ParentEnd"/>

<event title="Pixel VO" reconcileKey="106251137"/>


+<mediaStream>
<media Arg8="" Arg7="" Arg6="" Arg5="" Arg4="" Arg3="O1T13810" Arg2="1932" Arg1="TUE" RuleCode="2VO"/>
</properties>
</event>

and the code is below here:

static void Main(string[] args)
    {
        XmlDocument xdoc = new XmlDocument();

        xdoc.Load(@"C:\Users\namokhtar\Desktop\testxml.xml");

        foreach (XmlNode node in xdoc.SelectNodes("//event[@type='2VO']")) //or /CATALOG/CD

        {

            var x = node.SelectSingleNode("//@Arg1").Value;
            var y = node.SelectSingleNode("//@Arg2").Value;
            var z = node.SelectSingleNode("//@Arg3").Value;

            Console.WriteLine("The first parameter is: " + x);
            Console.WriteLine("The first parameter is: " + y);
            Console.WriteLine("The first parameter is: " + z);
            Console.ReadKey();
        }

Upvotes: 1

Views: 43

Answers (1)

spodger
spodger

Reputation: 1679

The problem is that the "//" in "//@Arg1" is telling it to look anywhere in the document not specifically the node you have selected so even thought you extract each <event> node, you then always get the first "//@Arg1" attribute value in the document.

Either use ".//@Arg1" to tell it to search relative to the current node or be more specific with your XPath and use "./properties/media/@Arg1"

If it's any consolation, I don't find this "//" behaviour intuitive!

Upvotes: 1

Related Questions