Solved

Create XML

Posted on 2016-11-21
5
55 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 22

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 22

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 63

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 63

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

729 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