Solved

Create XML

Posted on 2016-11-21
5
48 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

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

815 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

13 Experts available now in Live!

Get 1:1 Help Now