0

I currently have this XML schema :

<PSC5>
  <POI_ORI>
    <CIT>LIM</CIT>
  </POI_ORI>
</PSC5>

if user want to add a new option, the final schema will be :

<PSC5>
    <OPT>132<OPT>
  <POI_ORI>
    <CIT>LIM</CIT>
  </POI_ORI>
</PSC5>

if not just keep like :

<PSC5>
  <POI_ORI>
    <CIT>string</CIT>
  </POI_ORI>
</PSC5>

Im using the following snippet :

Dim oXMLDocument As New XmlDocument
            oXMLDocument.Load(strFileSchemaAWEB)

            Dim oNavigator As XPath.XPathNavigator = oXMLDocument.CreateNavigator() 

If not dtbParameters.Rows( 0 ).Item(5).equals("") Then
                oNavigator.AppendChild("<OPT>16</OPT>")

it throws me an exception on AppendChild, when I try to generate the second XML Schemma :

this document already has a 'DocumentElement' node.

Im Using VB.NET Framework 2.0

Thanks for help,

3 Answers 3

1

create the navigator on the root node of your XmlDocument

Dim root as XmlElement = oXMLDocument.DocumentElement
Dim oNavigator As XPath.XPathNavigator = root.CreateNavigator()
Sign up to request clarification or add additional context in comments.

Comments

0

The problem has nothing to do with the node being optional, by adding a node the way you did the XML becomes invalid (multiple DocumentElement).

Try adding your node to the location you want to.
You need to move the navigator object to the place you want to add the node.
Read about it here: AppendChildElement

Comments

0

Try this:

If Not dtbParameters.Rows( 0 ).Item(5).equals("") Then

   oXMLDocument.SelectSingleNode("/PSC5").AppendChild( _
      oXMLDocument.CreateElement("OPT")).InnerText = "16"

End If

HTH

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.