JenPann
JenPann

Reputation: 61

XML check if attribute does not exist

I have an XML file with faultIsolationProcedure nodes that have and do not have applicRefId attributes. I need to check if the attribute exists in the faultIsolationProcedure node. If it does not exist it needs to get the child fault elements faultCode attribute and create poplulate the faultIsolationProcedure's applicRefID with it.

This is my code but it's not working. I wanted hasAttr to be boolean but I received a type error from it.

code

      For Each node As XmlNode In doc.SelectNodes("/dmodule/content/faultIsolation/faultIsolationProcedure")
        Dim hasAttr = node.Attributes["applicRefId"] != null
        If hasAttr! = null Then

            Me.ListBox1.Items.Add(String.Format("{0}",
                                            node.Attributes("applicRefId").InnerText))
        End If
    Next

Sample XML

<content>
    <faultIsolation>
        <faultIsolationProcedure applicRefId="Blk1">
            <fault faultCode="48-038"/>
            <faultDescr>
                <descr>xxx xx</descr>
            </faultDescr>
            <isolationProcedure>
                <isolationMainProcedure>
                    <isolationStep id="i-001">
                        <action>xxx xxxxx xx</action>
                        <isolationStepQuestion>xxx xx</isolationStepQuestion>
                        <isolationStepAnswer>
                            <yesNoAnswer>
                                <yesAnswer nextActionRefId="e-001"/>
                                <noAnswer nextActionRefId="s-001"/>
                            </yesNoAnswer>
                        </isolationStepAnswer>
                    </isolationStep>
                    <isolationProcedureEnd id="e-001">
                        <action>xxx xxxxx xx</action>
                        <action>xxx xx</action>
                    </isolationProcedureEnd>
                </isolationMainProcedure>
            </isolationProcedure>
        </faultIsolationProcedure>
        <faultIsolationProcedure>
            <fault faultCode="48-039"/>
            <faultDescr>
                <descr>xxx xx</descr>
            </faultDescr>
            <isolationProcedure>
                <isolationMainProcedure>
                    <isolationStep id="i-001">
                        <action>xxx xxxxx xx</action>
                        <isolationStepQuestion>xxx xx</isolationStepQuestion>
                        <isolationStepAnswer>
                            <yesNoAnswer>
                                <yesAnswer nextActionRefId="e-001"/>
                                <noAnswer nextActionRefId="s-001"/>
                            </yesNoAnswer>
                        </isolationStepAnswer>
                    </isolationStep>
                    <isolationProcedureEnd id="e-001">
                        <action>xxx xxxx xx</action>
                        <action>xxx xx<dmRef>
                        </dmRef>xxx xx</action>
                    </isolationProcedureEnd>
                </isolationMainProcedure>
            </isolationProcedure>
        </faultIsolationProcedure>
    </faultIsolation>
</content>

Upvotes: 0

Views: 173

Answers (1)

jdweng
jdweng

Reputation: 34433

Try xml linq :

Imports System.Xml
Imports System.Xml.Linq
Module Module1
    Const FILENAME As String = "c:\temp\test.xml"
    Sub Main()
        Dim doc As XDocument = XDocument.Load(FILENAME)

        Dim faultIsolationProcedures As List(Of XElement) = doc.Descendants("faultIsolationProcedure").Where(Function(x) x.Attribute("applicRefId") Is Nothing).ToList()

        For Each faultIsolationProcedure In faultIsolationProcedures
            faultIsolationProcedure.SetAttributeValue("applicRefId", "Blk1")
        Next faultIsolationProcedure
    End Sub

End Module

Upvotes: 1

Related Questions