0

i got a XML File as export from Wireshark and want to select the Number of the actual frame

The structure of this file is like this

<packet>
    <proto>
        ...
    </proto>
    ....
    <proto>
        <field name="frame.number" show="1">
    </proto>
</packet>
<packet>
    <proto>
        ...
    </proto>
    ....
    <proto>
        <field name="frame.number" show="2">
    </proto>
</packet>

...and so on...

I use this code to select the packets/fields

XmlNodeList packages = xmlDoc.SelectNodes("//packet");
foreach (XmlNode packet in packages) {
    string frameNumber = packet.SelectSingleNode("//field[@name='frame.number']").
        Attributes["show"].Value;
    Console.WriteLine(frameNumber);
}

If I Debug through the code, it always selects the right Nodes with the correct attributes. But at each iteration there is a "1" printed out.

Does anyone suspect what failure this is? I didn't found anything on the internet for this failure

Thank you very much!!

1 Answer 1

2

Its because your XPath in SelectSingleNode starts with // - which means "start from the root of the document". Therefore you're always getting the first one.

Just change the XPath in that method to proto/field[@name='frame.number'].

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I thougt starting with // means: "start from the actual node downwards. Now I know, that I was wrong ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.