0

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;

}
}

我正在做一个 windows phone 7 应用程序,我希望使用 WCF 从 XML 文件中检索并附加到 XML 文件。我遇到了“不支持 URI 格式”的错误。当我希望保存这行“xmlDoc.Save(” http://localhost:57833/DataProvider/XML/User.xml“);”的 XML 时。似乎 WCF 无法在服务器中附加 XML 文件。

4

1 回答 1

0

您不能附加到远程服务器上的文件,而只能附加到本地磁盘上。因此,如果上面的代码在您要保存 xml 的机器上执行,请使用:

xmlDoc.Save("c:\\User.xml")

如果您不在该服务器上(并且无法通过 unc 访问它),那么您需要将文件上传到该计算机上的另一个 wcf 服务,以便将其保存在本地。

于 2013-07-28T00:03:35.107 回答