tags:

views:

33

answers:

3

Hello, 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,

A: 

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

Dror
+1  A: 

create the navigator on the root node of your XmlDocument

Dim root as XmlElement = oXMLDocument.DocumentElement
Dim oNavigator As XPath.XPathNavigator = root.CreateNavigator()
Gregoire
A: 

Try this:

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

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

End If

HTH

Rubens Farias