2013-07-27 30 views
0

WCFService.cs追加XML使用WCF

[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文件。而且我遇到了“不支持URI格式”的错误。当我希望保存XML“xmlDoc.Save(”http://localhost:57833/DataProvider/XML/User.xml“);”。看起来WCF不能在服务器中附加XML文件。

回答

0

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

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

如果您不是该服务器上(而不是通过UNC访问),那么你需要上传的文件到该机器上的另一个wcf服务,以便将其保存到本地。