Solved

Create XML

Posted on 2016-11-21
5
46 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
ID: 41896854
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
ID: 41896855
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
ID: 41896858
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
ID: 41896869
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
ID: 41896888
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 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

22 Experts available now in Live!

Get 1:1 Help Now