Results 1 to 4 of 4

Thread: [RESOLVED] Basic Writing to XML

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2007
    Posts
    11

    Resolved [RESOLVED] Basic Writing to XML

    Hi everyone,

    I have some data which I need to export into an XML file. The file syntax is:


    HTML Code:
    <functions type="0">
      <plugin name="Astring">
         <plugindomain name="TEXT">
           <computer name="TEXT" optionstxt="TEXT" note="TEXT" /> 
           <computer name="TEXT" optionstxt="TEXT" note="TEXT" /> 
         </plugindomain>
      </plugin>
      <plugin name="Bstring">
         <plugindomain name="TEXT">
           <computer name="TEXT" optionstxt="TEXT" note="TEXT" /> 
           <computer name="TEXT" optionstxt="TEXT" note="TEXT" /> 
         </plugindomain>
      </plugin>
    </functions>

    The data I have is listed in an EXCEL sheet and I can loop through all the different data without problem. I just do not know how to write to XML with elements, nodes etc

    The above shows two plugin sections, which will contain another child called plugindomain, under the plugindomain node I need to then add a load of computer nodes.

    Is there a way to complete this? I have never used XML before progrmatically with VB2005, so any assistance is greatly received!

    Thanks,

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Basic Writing to XML

    vb.net Code:
    1. Using writer As XmlWriter = XmlWriter.Create("file path here")
    2.     writer.WriteStartElement("functions")
    3.     writer.WriteAttributeString("type", "0")
    4.     writer.WriteStartElement("plugin")
    5.     writer.WriteAttributeString("name", "Astring")
    6.     writer.WriteStartElement("plugindomain")
    7.     writer.WriteAttributeString("name", "TEXT")
    8.     writer.WriteStartElement("computer")
    9.     writer.WriteAttributeString("name", "TEXT")
    10.     writer.WriteAttributeString("optionstxt", "TEXT")
    11.     writer.WriteAttributeString("note", "TEXT")
    12.     writer.WriteEndElement()
    13.     writer.WriteStartElement("computer")
    14.     writer.WriteAttributeString("name", "TEXT")
    15.     writer.WriteAttributeString("optionstxt", "TEXT")
    16.     writer.WriteAttributeString("note", "TEXT")
    17.     writer.WriteEndElement()
    18.     writer.WriteEndElement()
    19.     writer.WriteEndElement()
    20.     writer.WriteStartElement("plugin")
    21.     writer.WriteAttributeString("name", "Bstring")
    22.     writer.WriteStartElement("plugindomain")
    23.     writer.WriteAttributeString("name", "TEXT")
    24.     writer.WriteStartElement("computer")
    25.     writer.WriteAttributeString("name", "TEXT")
    26.     writer.WriteAttributeString("optionstxt", "TEXT")
    27.     writer.WriteAttributeString("note", "TEXT")
    28.     writer.WriteEndElement()
    29.     writer.WriteStartElement("computer")
    30.     writer.WriteAttributeString("name", "TEXT")
    31.     writer.WriteAttributeString("optionstxt", "TEXT")
    32.     writer.WriteAttributeString("note", "TEXT")
    33.     writer.WriteEndElement()
    34.     writer.WriteEndElement()
    35.     writer.WriteEndElement()
    36.     writer.WriteEndElement()
    37. End Using
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Basic Writing to XML

    Just note that that code will specifically create the XML you posted. Obviously you'll need to generalise the code to handle the general case. For instance, you have multiple 'plugin' elements so you would need to use a loop to create them. You also have multiple 'computer' elements so you'd need to nest another loop to create them.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2007
    Posts
    11

    Re: Basic Writing to XML

    Thanks jmcilhinney!! Just what I was looking for.

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