Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My project structure is like below
1. ApplicationUI is one project
2. Service is another library project
3. Repository is db level library project

and i have added the xml file in Repository level like below
Repository/XMLUserDetails.xml

when i tried to insert the data by using the below code then i am getting the error like this "
Could not find a part of the path
"

Not able to insert the data into xml whne i have maintained xml in Repository level.

What I have tried:

string xmlFileName = "~\\XMLUserDatabase.xml";
                XmlDocument oXmlDocument = new XmlDocument();
                oXmlDocument.Load(HttpContext.Current.Server.MapPath(xmlFileName));
                XmlNodeList nodelist = oXmlDocument.GetElementsByTagName("UsrDetails");
                XDocument xmlDoc = XDocument.Load(HttpContext.Current.Server.MapPath(xmlFileName));
                xmlDoc.Element("UsrDetails")
                    .Add(   new XElement("EmailAddress", objUserDetailsRequest.EmailAddress), 
                            new XElement("Password", objUserDetailsRequest.Password));
                xmlDoc.Save(HttpContext.Current.Server.MapPath(xmlFileName));


System.Xml.XmlDocument myXml = new System.Xml.XmlDocument();
                myXml.Load(System.Web.Hosting.HostingEnvironment.MapPath("~/XMLUserDetails.xml"));
                System.Xml.XmlNode xmlNode = myXml.DocumentElement.FirstChild;
                System.Xml.XmlElement xmlElement = myXml.CreateElement("UserDetails");

                xmlElement.SetAttribute("EmailAddress", objUserDetailsRequest.EmailAddress);
                xmlElement.SetAttribute("Password", objUserDetailsRequest.Password);
               

                myXml.DocumentElement.InsertBefore(xmlElement, xmlNode);
                myXml.Save(System.Web.Hosting.HostingEnvironment.MapPath("~/XMLUserDetails.xml"));

                 XmlTextReader xmlReader = new XmlTextReader(System.Web.Hosting.HostingEnvironment.MapPath("~/XMLUserDetails.xml"));
        xmlReader.Close();
Posted
Updated 10-Mar-19 4:14am

1 solution

The file isn't at the location you are looking for it, which is the root of your website. We can't access your server so we can't you why, only you can do that. Either the file isn't there at all or it is in a different folder to where you're looking.
 
Share this answer
 
Comments
DGKumar 11-Mar-19 7:07am    
Hi Have tried to get the file in repository with full path even that also getting same exception

Is it possible to use xml file in repository level using like below

System.Web.Hosting.HostingEnvironment.MapPath("D:/Mypplication/MyProject/Repository/XMLFiles/XMLUserDetails.xml")

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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