您好,在此先感谢您,我正在尝试从 silverlight 应用程序中的文本框和单击按钮触发的事件中获取输入,将它们转换为 xml 字符串,将字符串和指定的文件名传递给 WCF 服务调用并在该调用中将 xml 保存到指定的文件(通过字符串参数)。将文本捕获到 xml 字符串中的代码似乎可以成功运行(基于我在调试时在变量中看到的内容),如下所示:
private void ServerInfoNext_Click(object sender, RoutedEventArgs e)
{
//new RegisterServerGroupObject instance
RegisterServerGroupObject groupInfo= new RegisterServerGroupObject(groupNameTB.Text,1,parentServerNameTB.Text,LeaderNameCB.SelectedItem.ToString());
var serializer = new XmlSerializer(typeof(RegisterServerGroupObject));
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("","");
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = Encoding.UTF8;
settings.Indent = true;
settings.CloseOutput = true;
StringBuilder sb = new StringBuilder();
using (XmlWriter writer = XmlWriter.Create(sb,settings))
{
serializer.Serialize(writer, groupInfo);
writer.Close();
}
//sb now contains the xml string with the information from the serialized class
string contentsString = sb.ToString();
//create instance of XmlWrite service
XMLWriteServiceClient xmlClient = new XMLWriteServiceClient();
xmlClient.WriteXmlToServerCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(xmlClient_WriteXmlToServerCompleted);
xmlClient.WriteXmlToServerAsync("ServerGroups.xml", contentsString);
}
此时,当变量内容字符串传递给服务方法时,我可以看到它具有有效的 xml,以及服务方法本身,如下所示:
public class XMLWriteService : IXMLWriteService
{
public void WriteXmlToServer(string filename,string xmlString)
{
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(xmlString.ToString());
try
{
xDoc.Save(filename);
}
catch (FileNotFoundException e)
{
Console.WriteLine(e.InnerException.ToString());
}
}
}
try/catch 块并不表示找不到文件(“ServerGroups.xml”),我目前在项目的服务器端部分的 ClientBin 中有该 xml 文件。(.Web 端)。但是,在方法终止后,没有新的 xml 写入文件。有人可以告诉我我做错了什么吗?我不知道为什么 XmlDocument 类实例没有将其内容保存到文件中。提前致谢!