Results 1 to 4 of 4

Thread: [RESOLVED] VB.net Add a Child Node in an XML File

  1. #1

    Thread Starter
    Lively Member ThatSamiam's Avatar
    Join Date
    Apr 2007
    Posts
    125

    Resolved [RESOLVED] VB.net Add a Child Node in an XML File

    Hi,

    I have an XML file populated like this (from an inherited a project):
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <Root>
      <Info>
        <I1>iVal1=</I1>
        <I2>iVal2=</I2>
        <I3>iVal3</I3>
      </Info>
      <Data>
        <d1>dVal1=</d1>
        <d2>dVal2</d2>
      </Data>
    </Root>
    I would like to add another node to have this:
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <Root>
      <Condition1>
        <Info>
          <I1>iVal1=</I1>
          <I2>iVal2=</I2>
          <I3>iVal3</I3>
        </Info>
        <Data>
          <d1>dVal1=</d1>
          <d2>dVal2</d2>
        </Data>
      <\Condition1>
      <Condition2>
        <Info>
          <I1>a=</I1>
          <I2>b=</I2>
          <I3>c</I3>
        </Info>
        <Data>
          <d1>x=</d1>
          <d2>y</d2>
        </Data>
      <\Condition2>
    </Root>
    I have tried different approaches to add the node without success.
    Blocks 1 and 2 result in exception: System.InvalidOperationException: 'This document already has a 'DocumentElement' node.'

    Block 3 adds the condition element, but not the end element.
    Code:
     Public Sub WriteConfigAttribute(condition As String, iType As String, attr As String, attrValue As String, Optional root As String = "Root")
    
        Dim xdoc As New XmlDocument()
        xdoc.Load(Me.xmlFileName)
    	
        Dim n0 = xdoc.SelectSingleNode($"/{root}")
      
        ' Block 1
        Dim n1 As XmlNode = xdoc.CreateNode(XmlNodeType.Element, condition, Nothing)
        n0.ParentNode.AppendChild(n1)
    
        ' Block 2
        Dim e = xdoc.CreateElement(condition, Nothing)
        n0.ParentNode.InsertAfter(e, n0)
        n0.InsertBefore(n0, Nothing)
    	
        ' Block 3
        Dim newNode As XmlNode = xdoc.CreateNode(XmlNodeType.Element, condition, Nothing)
        xdoc.DocumentElement.AppendChild(newNode)
    
        xdoc.Save(Me.xmlFileName)
    
        n0 = xdoc.SelectSingleNode($"/{root}/{condition}")
    
        <rest of the code omitted for brevity>
    
        xdoc.Save(Me.xmlFileName)
      End Sub
    Any suggestions will be greatly appreciated.

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: VB.net Add a Child Node in an XML File

    You are using the wrong slash character in your Condition closing tags.

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: VB.net Add a Child Node in an XML File

    A little XElement, LINQ, and XML literals and voila,

    Code:
            Dim xdoc As XElement
            'from file
            '  xdoc = XElement.Load(Me.xmlFileName)
    
            'test using literal.  delete before using .Load
            xdoc = <Root>
                       <Info>
                           <I1>iVal1=</I1>
                           <I2>iVal2=</I2>
                           <I3>iVal3</I3>
                       </Info>
                       <Data>
                           <d1>dVal1=</d1>
                           <d2>dVal2</d2>
                       </Data>
                   </Root>
    
            Dim newDoc As XElement = <Root>
                                         <Condition1>
                                             <%= From el In xdoc.Elements Select el %>
                                         </Condition1>
                                         <Condition2>
                                             <Info>
                                                 <I1>a=</I1>
                                                 <I2>b=</I2>
                                                 <I3>c</I3>
                                             </Info>
                                             <Data>
                                                 <d1>x=</d1>
                                                 <d2>y=</d2>
                                             </Data></Condition2>
                                     </Root>
    
            ' newDoc contains what you want
            'newDoc.Save("PATH HERE")
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4

    Thread Starter
    Lively Member ThatSamiam's Avatar
    Join Date
    Apr 2007
    Posts
    125

    Re: VB.net Add a Child Node in an XML File

    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width