Solved

Create XML

Posted on 2016-11-21
5
30 Views
Last Modified: 2016-11-21
Hi Experts,
     I was loking for the vb.net code to create XML as follows  and save it in an XML  file
      (Alternatively, c# code is also fine)

<MyXML SoftwareVersion='2.1' CreationUser='TestUser' CreationDate='Now'>
  <MyElement>
    <MyObject Name='Test1' Description = 'This is test' />
    <IFile Name='TestFile' Description = 'Test File'/>  
  </MyElement>
</MyXML>
0
Comment
Question by:Sam OZ
  • 2
  • 2
5 Comments
 
LVL 21

Expert Comment

by:JesterToo
Comment Utility
Have a look at the example code at this link...   http://vb.net-informations.com/xml/create-xml-vb.net.htm
0
 

Author Comment

by:Sam OZ
Comment Utility
Thanks. But I can't have the first line <?xml Version=1.0 ..... />
 It has to be  the in the format of  XML I had shown
0
 
LVL 21

Expert Comment

by:JesterToo
Comment Utility
Well, it won't technically be a "well formed XML" file.  If you just want a file that looks mostly like xml format then just use the normal streamwriter like in this example:  https://msdn.microsoft.com/en-us/library/system.io.file.createtext(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2

Click on the "VB" tab to see the example writen in vb.net if it doesn't already show that.
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
Hi Sam;

This should do what you need.
Imports System.Xml.Linq

'' Create the XElements to be added to an XML document
Dim xelement = <MyXML SoftwareVersion="2.1" CreationUser="TestUser" CreationDate="Now">
                   <MyElement>
                       <MyObject Name="Test1" Description="This is test"/>
                       <IFile Name="TestFile" Description="Test File"/>
                   </MyElement>
               </MyXML>

'' Create the XML document using the above the XML elements
Dim xdoc = New XDocument(xelement)
'' Save the XML to the file system using a filepath and file name
xdoc.Save("MyXmlDoc.xml")

Open in new window

0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Hi Sam;

To your statement, "Thanks. But I can't have the first line <?xml Version=1.0 ..... /> It has to be  the in the format of  XML I had shown", as JesterToo stated it will NOT be a "well formed XML". That stated you can do this.
Imports System.Xml.Linq

'' Create the XElements to be added to an XML document
Dim xelement = <MyXML SoftwareVersion="2.1" CreationUser="TestUser" CreationDate="Now">
                   <MyElement>
                       <MyObject Name="Test1" Description="This is test"/>
                       <IFile Name="TestFile" Description="Test File"/>
                   </MyElement>
               </MyXML>

'' Save the XML elements as a standard text file to MyXmlNodes.xml 
File.WriteAllText("MyXmlNodes.xml", xelement.ToString())

Open in new window

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now