-4

I wants to create an xml like.

<?xml version="1.0" encoding="utf-8"?>   
<Initials>ANP</Initials>
<MedicalPracticeName>Dr. Anny</MedicalPracticeName>
<Start>2016-11-17</Start>
<Logon>
   <UserId>admin</UserId>
   <Password>abcde</Password>
</Logon>

by using XMLDocument class.

I have tried this code, but didn't success

XmlDocument xmlDoc = new XmlDocument();
XmlNode firstNode = xmlDoc.CreateNode(XmlNodeType.Element,"Initials","http://www.google.com");
xmlDoc.AppendChild(firstNode );

firstNode.InnerText = "ANP";

XmlNode SecondNode = xmlDoc.CreateNode(XmlNodeType.Element, "MedicalPracticeName", "http://www.google.com");
xmlDoc.AppendChild(SecondNode);

SecondNode.InnerText = "Dr. Anny";

XmlNode ThirdNode = xmlDoc.CreateNode(XmlNodeType.Element, "Start", "http://www.google.com");
xmlDoc.AppendChild(ThirdNode );

ThirdNode.InnerText = "2016-11-17";
Michał Żołnieruk
  • 2,095
  • 12
  • 20
Lets Do Green
  • 127
  • 11
  • 3
    Your Xml sample is not well formed. Why would you want to do something like this? – JuanR Apr 30 '18 at 13:00
  • 4
    You can't. A valid Xml document requires exactly 1 root element. – Filburt Apr 30 '18 at 13:00
  • Possible duplicate of [How to Remove Root Element in C#/](https://stackoverflow.com/questions/17503619/how-to-remove-root-element-in-c) – JuanR Apr 30 '18 at 13:15
  • I understand XMLDocument must have only one root node. But any other way, how to create the above XML. like concatenating different XMLNodes to one single XMLDocument. – Lets Do Green Apr 30 '18 at 13:47

1 Answers1

1

Your code looks fine, your issue is with XML itself, you should follow some standards, or use another format if you can. If you don't follow it, you or anyone else working with the files in the future may have some trouble to work with it.

You may check this question and also the W3C specifications

GAltelino
  • 95
  • 2
  • 12