Parse XML without namespace

StewartBW 845 Reputation points
2024-07-11T09:52:15.3266667+00:00

Hello

When parsing this kind of xml, I need to get the number of elementCount and the following 2 fields:

OPFCategoryCopyBackgroundColor

OPFCategoryCopyName

Here's my working code:

Dim MyXML As XElement = XElement.Load(MemoryStream)
Dim count As Integer = Integer.Parse(MyXML.@elementCount)
For Each MyXElement As XElement In MyXML.Elements
Dim Components As String() = MyXElement.@OPFCategoryCopyBackgroundColor.Split()
R = Single.Parse(Components(0).Substring(2))
G = Single.Parse(Components(1).Substring(2))
B = Single.Parse(Components(2).Substring(2))
EName = MyXElement.@OPFCategoryCopyName

Is it possible to obtain the above 3 fields using XmlDocument rather than XElement?

Xml sample:


<?xml version="1.0" encoding="utf-8"?>
<categories elementCount="18">
  <category xml:space="preserve" OPFCategoryCopyBackgroundColor="r:0.57 g:0.48 b:0.82" OPFCategoryCopyName="Birthday"></category>
  <category xml:space="preserve" OPFCategoryCopyBackgroundColor="r:0.45 g:0.60 b:0.88" OPFCategoryCopyName="Blue category"></category>
  <category xml:space="preserve" OPFCategoryCopyBackgroundColor="r:0.92 g:0.79 b:0.40" OPFCategoryCopyName="Brown Category"></category>
  <category xml:space="preserve" OPFCategoryCopyBackgroundColor="r:0.63 g:0.26 b:0.80" OPFCategoryCopyName="Family"></category>
</categories>

Thanks in advance :)

  • Update, found this:
For Each el As XmlNode In doc.SelectNodes("/categories/category")
        MsgBox(el.Attributes.GetNamedItem("OPFCategoryCopyBackgroundColor").Value)
Next

The only question, both following codes will work:

el.Attributes.GetNamedItem("OPFCategoryCopyBackgroundColor").Value
vs
el.Attributes("OPFCategoryCopyBackgroundColor").Value

So which one is safer or recommended to use?

Thanks :)

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,659 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 28,946 Reputation points Microsoft Vendor
    2024-07-12T01:55:03.26+00:00

    Hi @StewartBW ,

    1. el.Attributes.GetNamedItem("OPFCategoryCopyBackgroundColor").Value:
      • Uses GetNamedItem method to get the attribute by name.
    2. el.Attributes("OPFCategoryCopyBackgroundColor").Value:
      • Uses the default Attributes collection's indexer to get the attribute by name, which is more concise.

    Both approaches are valid, but the el.Attributes("OPFCategoryCopyBackgroundColor").Value method is shorter and commonly used, making it a bit more readable and recommended.

    Best Regards.

    Jiachen Li


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful