Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET XML
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 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 10-Sep-12 20:17pm
Edited 10-Sep-12 20:47pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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);
  Permalink  
Comments
biswarup88 at 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 at 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);
biswarup88 at 11-Sep-12 3:50am
   
Thanks a lot Kuthuparakkal
Kuthuparakkal at 11-Sep-12 4:00am
   
you're most welcome!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
biswarup88 at 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)

  Print Answers RSS
0 OriginalGriff 349
1 Nirav Prabtani 268
2 Richard Deeming 215
3 CHill60 170
4 _Amy 145
0 OriginalGriff 8,104
1 Sergey Alexandrovich Kryukov 7,045
2 Maciej Los 4,039
3 Peter Leow 3,738
4 CHill60 2,912


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 11 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100