0

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>

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.