2

I guys, I have a xml with this model:

<XML>
  <IdValue id="1">
    <Value a="1" b="2" c="3" />
  </IdValue >
  <IdValue id="2">
    <Value a="10" b="20" c="30" />
  </IdValue >
</XML>

So i want to read, with a VB.NET program, the value of the attributes a, b, and c, in based on the value of the attribute id of the node IdValue.

Can yo help me ?

Thanks :)

2
  • Have you tried something? Commented Mar 27, 2013 at 19:13
  • No, sorry. It's the first time that i used XML files Commented Mar 27, 2013 at 19:19

1 Answer 1

1

It's quite easy using VB.Net's XML literals and Linq-to-XML.

Given the following XML:

Dim xml = <XML>
            <IdValue id="1">
                <Value a="1" b="2" c="3" />
            </IdValue >
            <IdValue id="2">
                <Value a="10" b="20" c="30" />
            </IdValue >
           </XML>

You can extract the values you are looking for as easy as

Dim result = From id In xml.<IdValue>
             Select New With { .Id = id.@id,
                               .a  = id.<Value>.@a, 
                               .b  = id.<Value>.@b,
                               .c  = id.<Value>.@c }

For Each item in result
    Console.WriteLine(String.Format("id:{0} a:{1} b:{2} c:{3}", item.id, item.a, item.b, item.c))
Next

Output:

id:1 a:1 b:2 c:3
id:2 a:10 b:20 c:30

If you're interested in a single <IdValue>, you can add a Where clause; for example:

Dim result = From id In xml.<IdValue>
             Where id.@id = 2
             Select New With { .Id = id.@id,
                               .a  = id.<Value>.@a, 
                               .b  = id.<Value>.@b,
                               .c  = id.<Value>.@c }
Sign up to request clarification or add additional context in comments.

Comments

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.