-
Jul 17th, 2024, 05:50 AM
#1
Thread Starter
Member
Get Value from XML based on Attribute number
Hi all,
I want to return the value where attribute is 15, from the xml data below. Is there a way i can search the xml and find 'attr num = 15' and then return the value. I would sooner search for the 'attr num = 15' rather than locate it with the path e.g. \MyCompoundDocument\MyAttributes\.. etc.. because this path may change in the future.
Code:
<?xml version="1.0" encoding="UTF-8"?>
<MyCompoundDocument xmlns="">
<MyAttributes>
<Group class="system" name="System" desc="These attributes describe the My system used to create this file."
ord="8">
<Attr num="13" name="Language" desc="My language code." type="i" ord="1" value="0">
<Valid perm="r"/>
</Attr>
<Attr num="14" name="Licence" desc="My licence code." type="s" ord="2" value="9735-8923-3075-8201-3618-0243">
<Valid perm="r"/>
</Attr>
<Attr num="15" name="Build" desc="My software build." type="s" ord="3" value="2023.1.2323">
<Valid perm="r"/>
</Attr>
</Group>
<Group class="custom"
-
Jul 17th, 2024, 06:43 AM
#2
Re: Get Value from XML based on Attribute number
Haven't tested by something like?:
Code:
xmlNodeList = xmlDcoument.DocumentElement.SelectNodes("/MyCompoundDocument/MyAttributes/")
For Each xmlNode As XmlNode In xmlNodeList
If xmlNode.Attributes("attr_num").Value = "15" Then
...
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jul 17th, 2024, 07:05 AM
#3
Thread Starter
Member
Re: Get Value from XML based on Attribute number
Thanks for the reply.
I tired this but the XMLNodeList has a count of 0 after the selectnode line.
Any suggestions would be greatly appreciated.
-
Jul 17th, 2024, 07:54 AM
#4
Re: Get Value from XML based on Attribute number
First off your file is not well formatted.
Try this.
Code:
<?xml version="1.0" encoding="UTF-8"?>
<MyCompoundDocument xmlns="">
<MyAttributes>
<Group class="system" name="System" desc="These attributes describe the My system used to create this file."
ord="8">
<Attr num="13" name="Language" desc="My language code." type="i" ord="1" value="0">
<Valid perm="r"/>
</Attr>
<Attr num="14" name="Licence" desc="My licence code." type="s" ord="2" value="9735-8923-3075-8201-3618-0243">
<Valid perm="r"/>
</Attr>
<Attr num="15" name="Build" desc="My software build." type="s" ord="3" value="2023.1.2323">
<Valid perm="r"/>
</Attr>
</Group>
</MyAttributes>
</MyCompoundDocument>
Then you need
Code:
xmlDcoument.DocumentElement.SelectNodes("/MyCompoundDocument/MyAttributes/Group/Attr")
For Each xmlNode As XmlNode In xmlNodeList
If xmlNode.Attributes("num").Value = "14" Then
.....
for more groups I Would guess take the /MyCompoundDocument/MyAttributes/Group/ element and iterate the attributes.
Last edited by sapator; Jul 17th, 2024 at 08:03 AM.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jul 17th, 2024, 08:01 AM
#5
Re: Get Value from XML based on Attribute number
OK.
Writing in C# as I code it but is almost the same in VB.
So
multiple groups xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<MyCompoundDocument xmlns="">
<MyAttributes>
<Group class="system" name="System" desc="These attributes describe the My system used to create this file."
ord="8">
<Attr num="13" name="Language" desc="My language code." type="i" ord="1" value="0">
<Valid perm="r"/>
</Attr>
<Attr num="14" name="Licence" desc="My licence code." type="s" ord="2" value="9735-8923-3075-8201-3618-0243">
<Valid perm="r"/>
</Attr>
<Attr num="15" name="Build" desc="My software build." type="s" ord="3" value="2023.1.2323">
<Valid perm="r"/>
</Attr>
</Group>
<Group class="system" name="System" desc="These attributes describe the My system used to create this file."
ord="8">
<Attr num="13" name="Language" desc="My language code." type="i" ord="1" value="0">
<Valid perm="r"/>
</Attr>
<Attr num="14" name="Licence" desc="My licence code." type="s" ord="2" value="9735-8923-3075-8201-3618-0243">
<Valid perm="r"/>
</Attr>
<Attr num="15" name="Build" desc="My software build." type="s" ord="3" value="2023.1.2323">
<Valid perm="r"/>
</Attr>
</Group>
</MyAttributes>
</MyCompoundDocument>
And the code:
Code:
XmlNodeList xmlNodeList = xmlDcoument.DocumentElement.SelectNodes("/MyCompoundDocument/MyAttributes/Group");
foreach (XmlNode xmlNode in xmlNodeList)
{
foreach (XmlNode xmlNode2 in xmlNode)
if (xmlNode2.Attributes["num"].Value == "14")
{
//do something
break;
}
}
}
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jul 17th, 2024, 12:28 PM
#6
Thread Starter
Member
Re: Get Value from XML based on Attribute number
Thanks but unfortunately this doesn't work. After line 1 the nodelist is empty.
Any other suggests please?
-
Jul 17th, 2024, 01:18 PM
#7
Re: Get Value from XML based on Attribute number
No it works.
I have wrote the code and it works fine.
Copy the EXACT xml file in a notepad and save it then LOAD it before using the code above.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jul 17th, 2024, 01:35 PM
#8
Thread Starter
Member
Re: Get Value from XML based on Attribute number
Hi, Sorry.. yes it does work when i copied the xml exactly.
I tired it on another xml file and it did not work. After some playing, i realised the second line in the xml file was different.
With <MyCompoundDocument xmlns=""> it works.. but if there is any value for xmlns.. it does not work
e.g. <MyCompoundDocument xmlns="a"> does not work..
Any ideas?
-
Jul 17th, 2024, 01:56 PM
#9
Re: Get Value from XML based on Attribute number
Not at work not on a VS PC so I have to test but why do you need the "a" ?
You said "I want to return the value where attribute" are you suspecting that there would be another "MyCompoundDocument" ? Since it seems that it's the root I would think not.
In any case if you need an exact parse you have to show an exact working xml document that you will be parsing else we are just fixing your code bits as we go along.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jul 17th, 2024, 02:01 PM
#10
Thread Starter
Member
Re: Get Value from XML based on Attribute number
Ok i understand. thank you
-
Jul 18th, 2024, 07:53 AM
#11
Thread Starter
Member
Re: Get Value from XML based on Attribute number
Hi all, Ok.. so here is the correct XML data:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<RadanCompoundDocument xmlns="http://www.Mytest.com/ns/rcd">
<RadanAttributes>
<Group class="system" name="System" desc="These attributes describe the MyTest system used to create this file."
ord="8">
<Attr num="13" name="Language" desc="MyTest language code." type="i" ord="1" value="0">
<Valid perm="r"/>
</Attr>
<Attr num="14" name="Licence" desc="MyTest licence code." type="s" ord="2" value="9735">
<Valid perm="r"/>
</Attr>
<Attr num="15" name="Build" desc="MyTest oftware build." type="s" ord="3" value="2023.1.2323">
<Valid perm="r"/>
</Attr>
</Group>
</RadanAttributes>
</RadanCompoundDocument>
The vb.net code doesn't work for this XML file. The nodelist is nothing.
In the XML file- If i change the second line from <RadanCompoundDocument xmlns="http://www.Mytest.com/ns/rcd"> to <RadanCompoundDocument xmlns="">
It works fine.
Any advise is appreciated.
-
Jul 18th, 2024, 08:05 AM
#12
Re: Get Value from XML based on Attribute number
Something like the following should work...
Code:
Dim xml = <?xml version="1.0" encoding="UTF-8"?>
<RadanCompoundDocument xmlns="http://www.Mytest.com/ns/rcd">
<RadanAttributes>
<Group class="system" name="System" desc="These attributes describe the MyTest system used to create this file."
ord="8">
<Attr num="13" name="Language" desc="MyTest language code." type="i" ord="1" value="0">
<Valid perm="r"/>
</Attr>
<Attr num="14" name="Licence" desc="MyTest licence code." type="s" ord="2" value="9735">
<Valid perm="r"/>
</Attr>
<Attr num="15" name="Build" desc="MyTest oftware build." type="s" ord="3" value="2023.1.2323">
<Valid perm="r"/>
</Attr>
</Group>
</RadanAttributes>
</RadanCompoundDocument>
Dim rcd As XNamespace = "http://www.Mytest.com/ns/rcd"
Dim res = From ele In xml.Descendants(rcd + "Attr")
Where ele.Attribute("num").Value = 15
Select ele
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|