Solved

Create XML

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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

726 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