WCFService.cs
[AspNetCompatibilityRequirements(
RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class WCFService : IWCFService{
public Boolean insertUser(String name, String password)
{
Boolean successInsert = false;
XDocument xDoc = XDocument.Load("`http://localhost:57833/DataProvider/XML/User.xml`");
Boolean userExist = (from user in xDoc.Descendants("user")
where (String)user.Attribute("name") == name
select user).Any();
if (!userExist)
{
XElement root = xDoc.Root;
int lastUserId = Convert.ToInt16(root.Elements("user").Last().Attribute("id").Value);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("`http://localhost:57833/DataProvider/XML/User.xml`");
XmlNode xmlElementUser = xmlDoc.CreateNode(XmlNodeType.Element, "user", "");
XmlAttribute xmlAttributeUserID = xmlDoc.CreateAttribute("id");
XmlAttribute xmlAttributeName = xmlDoc.CreateAttribute("name");
XmlAttribute xmlAttributePassword = xmlDoc.CreateAttribute("password");
XmlAttribute xmlAttributeUserType = xmlDoc.CreateAttribute("userType");
xmlAttributeUserID.Value = (lastUserId + 1).ToString();
xmlAttributeName.Value = name;
xmlAttributePassword.Value = password;
xmlAttributeUserType.Value = "borrower";
xmlElementUser.Attributes.Append(xmlAttributeUserID);
xmlElementUser.Attributes.Append(xmlAttributeName);
xmlElementUser.Attributes.Append(xmlAttributePassword);
xmlElementUser.Attributes.Append(xmlAttributeUserType);
xmlDoc.DocumentElement.AppendChild(xmlElementUser);
xmlDoc.Save("`http://localhost:57833/DataProvider/XML/User.xml`");
successInsert = true;
}
return successInsert;
}
}
I'm doing a windows phone 7 app, and i wish to retrieve from and append to XML file by using WCF. And i encounter an error of "URI formats are not supported." when i wish to save the XML which is this line "xmlDoc.Save("http://localhost:57833/DataProvider/XML/User.xml
");". It seem like WCF cannot append the XML file in server.
You cannot append to a file on a remote server but only on the local disk. So if the code above is executed on the machine where you want to save the xml use:
xmlDoc.Save("c:\\User.xml")
If you are not on that server (and it is not accessible via unc) then you need to upload the file to another wcf service on that machine so it will save it locally.