Click here to Skip to main content
15,883,606 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Getting error while load xml document using c#; i want to insert records in xml document as well as update and delete records from that xml.
I have added a new xml file into my project inside App_Data folder and written this following code to insert record into it.
But in the load section i am getting these above error.
Here is my code snippet:
System.Xml.XmlDocument myXmlDocument = new System.Xml.XmlDocument();
myXmlDocument.Load(Server.MapPath("test.xml"));


Here is my test.xml format
XML
<?xml version="1.0" encoding="utf-8"?>
<entry>
  <Name name="test"></Name>
  <Location name="India"></Location>
  <Email name="aaa@test.com"></Email>
  <DOBirth name="01-02-1988"></DOBirth>
  <Gender name="M"></Gender>
</entry>
Posted
Updated 24-Aug-20 1:11am
v2

C#
System.Xml.XmlDocument myXmlDocument = new System.Xml.XmlDocument();
string xmlPath = System.IO.Path.Combine(Server.MapPath("~/App_Data"), "test.xml");
if(!File.Exists(xmlPath))
 throw new Exception("Cannot find xml");
myXmlDocument.Load(xmlPath);
 
Share this answer
 
Comments
sahabiswarup 11-Sep-12 3:08am    
It works perfectly; can you please tell me how to insert data into xml as tree view?

entry
name name="test"
location name="India"
email name="aaa@test.com" /email
dobirth name="01-02-1988" /dobirth
gender name="M" /gender
/location
/name
name name="test1"
location name="India"
email name="aaa@test.com" /email
dobirth name="01-05-1989" /dobirth
gender name="F" /gender
/location
/name
/entry
Kuthuparakkal 11-Sep-12 3:32am    
System.Xml.XmlDocument myXmlDocument = new System.Xml.XmlDocument();
string xmlPath = System.IO.Path.Combine(Server.MapPath("~/App_Data"), "test.xml");
if(!File.Exists(xmlPath))
throw new Exception("Cannot find xml");
myXmlDocument.Load(xmlPath);

XmlNode subRoot = myXmlDocument.DocumentElement.FirstChild;

XmlElement appendedElementUsername = myXmlDocument.CreateElement("Name");
XmlText xmlTextUserName = xmlDoc.CreateTextNode("India");
appendedElementUsername.AppendChild(xmlTextUserName);
subRoot.AppendChild(appendedElementUsername);
myXmlDocument.DocumentElement.AppendChild(subRoot);

repeat for other Location, Email etc.



myXmlDocument.Save(xmlPath);
sahabiswarup 11-Sep-12 3:50am    
Thanks a lot Kuthuparakkal
Kuthuparakkal 11-Sep-12 4:00am    
you're most welcome!
Primo Chalice 30-May-18 7:08am    
Hello,

Can you please tell me how do I use openFileDialog to load a XML file and show the data in RichTextBox?

I have tried the following:

OpenFileDialog openFileDialog1 = new OpenFileDialog();

openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
string open = openFileDialog1.FileName;

XmlDocument doc = new XmlDocument();
doc.LoadXml(open);
string xmlString = doc.InnerText;
RichTextBox2.Text = xmlString;
Did you happen to save the file in a different encoding? Try to save it again, and make sure that you explicitly selected UTF-8.
 
Share this answer
 
Comments
sahabiswarup 11-Sep-12 2:53am    
Here is my C# code
System.Xml.XmlDocument myXmlDocument = new System.Xml.XmlDocument();
myXmlDocument.Load(Server.MapPath("test.xml"));
System.Xml.XmlNode myXmlNode = myXmlDocument.DocumentElement.FirstChild;

System.Xml.XmlElement myXmlElement = myXmlDocument.CreateElement("entry");
myXmlElement.SetAttribute("Name", Server.HtmlEncode("aaa"));
myXmlElement.SetAttribute("Location", Server.HtmlEncode("India"));
myXmlElement.SetAttribute("Email", Server.HtmlEncode("aaa@gmail.com"));
myXmlElement.SetAttribute("DOBirth", Server.HtmlEncode("01-08-1988"));
myXmlElement.SetAttribute("Gender", Server.HtmlEncode("Male"));

myXmlDocument.DocumentElement.InsertBefore(myXmlElement, myXmlNode);
myXmlDocument.Save(Server.MapPath("test.xml"));

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900