-1

I am working with data saved in XML and looking to manage quite a lot of data.

In Visual Studio I have tried coding but, unfortunately, without success.

Sub neki()

    Dim objXLDoc As XDocument = XDocument.Load("c:\MyXml.xml")
    Dim objQuery = objXLDoc.Element("List").Elements("List2").Attributes("name")

    For Each objResult As XAttribute In objQuery
        Console.WriteLine(objResult.Name.ToString() & " = " & 
objResult.Value.ToString)

    Next

End Sub

XML sample

<?xml version="1.0" standalone="yes"?>
<MyInfo xmlns="http://www.myside.com">
<StandardHeader local="SPR" />
<List>
 <List2 MyInfoId="123" MyProd="Box" name="5-1" />
 <List2 MyInfoId="124" MyProd="Box1" name="5-2" />
 <List2 MyInfoId="125" MyProd="Box2" name="5-3" />
 <List2 MyInfoId="126" MyProd="Box3" name="5-4"/>
 <List2 MyInfoId="127" MyProd="Box4" name="5-5"/>
 <List2 MyInfoId="128" MyProd="Box5" name="5-6"/>
 <List2 MyInfoId="129" MyProd="Box6" name="5-7"/>
 <List2 MyInfoId="130" MyProd="Box7" name="5-8"/>
 <List2 MyInfoId="131" MyProd="Box8" name="5-9"/>
 <List2 MyInfoId="132" MyProd="Box9" name="5-10"/>
 </List>
</MyInfo>

I would like to read/import all data (MyInfoId, MyProd, name) for each "List2" row in the table.

TomazP
  • 11
  • 3
  • Define "without success". In what way does this code fail? – David May 09 '19 at 17:27
  • In the console, I do not receive any value from mentioned attributes (name, MyInfoId) – TomazP May 09 '19 at 17:31
  • When I test this exact code with this exact data I get an exception because the XML is invalid. The `` elements are never closed. How are you not getting that exception? – David May 09 '19 at 17:36
  • Sorry, I have made a mistake when copied. In my XML file, the "/" sign is present. I have also updated the post. – TomazP May 09 '19 at 17:40
  • In case it isn't clear from the duplicate, as it is in C#, you need `Dim xns As XNamespace = "http://www.myside.com"` and then `Dim objQuery = objXLDoc.Root.Element(xns + "List").Elements(xns + "List2").Attributes("name")`. – Andrew Morton May 09 '19 at 17:56

1 Answers1

0

You could make it easy on yourself and read it into a dataset/datatable IE

    Dim dset As New DataSet
    dset.ReadXml("c:\MyXml.xml", XmlReadMode.Auto)

then its just a simple matter of accessing your data through the datatables.

Mr. Tripodi
  • 809
  • 1
  • 6
  • 7