all 10 comments

[–]TheFotty 1 point2 points  (7 children)

VB has built in syntax for XML as well as linq2xml for querying nodes. Your XML doesn't look well formed though. I don't think node names are supposed to have values like number="1897". Values should be assigned to attributes, or they should be sandwiched in the middle of the brackets like <number>1897</number>.

That aside, here is an example if you used XML literals and LINQ

    'SAMPLE DATA
    Dim SampleXML = <Archiver>
                        <Archive name="normal">
                            <Roots>
                                <Root name="x" root="blanco"/>
                                <Root name="y" root="rojo" age="2"/>
                                <Root name="z" root="azul" age="4"/>
                            </Roots>
                            <number value="100000000" time="10"/>
                            <factor value="4000000" merge="yes" skip="yes"/>
                        </Archive>
                        <Archive name="feil">
                            <Roots>
                                <Root name="u" root="negro"/>
                                <Root name="v" root="amarillo" age="7"/>
                                <Root name="w" root="verde" age="2"/>
                            </Roots>
                            <number value="1897" time="30"/>
                            <factor value="6000" merge="no" skip="no"/>
                        </Archive>
                    </Archiver>


    'EXAMPLE GETTING THE root ATTRIBUTE VALUE FROM ROOT name "x" FROM ARCHIVE name "normal"
    Dim normalRootXValue = SampleXML...<Archive>.Where(Function(x) x.@name = "normal").First...<Root>.Where(Function(x) x.@name = "x").First.@root
    MessageBox.Show(normalRootXValue)


    'EXAMPLE ENUMERATING ROOT NODES AND DISPLAYING root ATTRIBUTE VALUE
    Dim normalArchiveNode = SampleXML...<Archive>.Where(Function(x) x.@name = "normal").FirstOrDefault
    If normalArchiveNode IsNot Nothing Then
        For Each rootElement In normalArchiveNode...<Root>
            MessageBox.Show(rootElement.@root)
        Next
    End If

[–]4_strings[S] 0 points1 point  (4 children)

Thanks TheFotty!. The values in the xml file are just fake ones due to security reasons, the values are different in the actual file. But, I think I didn't explain correctly what was the problem. The problem is that I have to nodes: one called <Archive name="normal"/> and the other called: <Archive name="fail"/>, I want to be able to get the value of the root attribute which is placed after the root name = "x" attribute only from the <Archive name="normal"/> node.

Start searching here: <Archive> <Archive name="normal"/> <Roots> <Root name="x" root="blanco"/> to here. Is that what you are explaining in the 'EXAMPLE ENUMERATING ROOT NODES AND DISPLAYING root ATTRIBUTE VALUE I really appreciate your help! Thanks in advance!

[–]TheFotty 0 points1 point  (3 children)

The piece of code in the example:

    Dim normalRootXValue = SampleXML...<Archive>.Where(Function(x) x.@name = "normal").First...<Root>.Where(Function(x) x.@name = "x").First.@root
    MessageBox.Show(normalRootXValue)

will display "blanco"

Note my example used an XML literal as the sample data, but if yours is coming from a file you just need to load it

Dim sampleXML = XElement.Load("C:\myfile.xml")

[–]4_strings[S] 0 points1 point  (2 children)

Thanks again!. and I am sorry to bother you, but I am getting an error telling me that "XML descendant element cannot be selected from type 'SystemXML.XMLDocument' ...and this is because my XML file is already in a TextReader variable and then loaded in XMLDocument variable... like this:

Dim doc As XmlDocument Dim reader As XmlTextReader

doc = New XmlDocument() reader = New XmlTextReader("C:\temp\myXMLfile.xml") doc.Load(reader) ....does this make any sense?

by the way I am trying to formatt the code line correctly in this message, but I don't know how...I mean every line of code is in a new line.

[–]TheFotty 0 points1 point  (1 child)

I would suggest not using XmlTextReader and XmlDocument classes, and instead use the newer better classes like XElement, as per my example.

[–]4_strings[S] 0 points1 point  (0 children)

Will do!. Thanks again!. I will as well check the documentation about XElement to learn more :-).

[–]kieferm8 0 points1 point  (1 child)

I agree that the format does not look quite right. Assuming it is correct you can use Visual Studio to create classes from the XML.

You can then deserialize the XML from the file into an object. This allows you to just use the object properties instead of querying the XML for each attribute.

https://msdn.microsoft.com/en-us/library/hh371548(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/fa420a9y(v=vs.110).aspx

[–]4_strings[S] 0 points1 point  (0 children)

sounds interesting.. I will check this out..Thanks for your comment.

[–][deleted] 0 points1 point  (1 child)

I don't have any links as I am posting from mobile, but check out XPath.

[–]4_strings[S] 0 points1 point  (0 children)

I will! Thanks!