我有mysoquest请求消息,该消息位于myrequest.xml文件中,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<get_waybill_by_number xmlns="http://tempuri.org/">
<su>UserName</su>
<sp>Password</sp>
<waybill_number>Number</waybill_number>
</get_waybill_by_number>
</soap:Body>
</soap:Envelope>
我想在相应的位置写特定的用户名,密码和运单号。我已经尝试使用几种类和方法(例如XmlDocument,XmlNode,XmlNamespaceManager)来实现此任务,但我什至无法获取数据:
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"C:\Users\D\desktop\myrequest.xml");
XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(xmlDocument.NameTable);
xmlNamespaceManager.AddNamespace("Soap", "http://schemas.xmlsoap.org/soap/envelope/");
xmlNamespaceManager.AddNamespace("temp", "http://tempuri.org/");
XmlNodeList nodeList = xmlDocument.SelectNodes("//Soap:body/temp:get_waybill_by_number",xmlNamespaceManager);
foreach (var item in nodeList)
{
Console.WriteLine(item);
}
答案 0 :(得分:1)
在这种情况下,您不需要添加“ Soap”名称空间。试试这个:
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"C:\Users\D\desktop\myrequest.xml");
XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(xmlDocument.NameTable);
xmlNamespaceManager.AddNamespace("temp", "http://tempuri.org/");
XmlNode xmlNode = xmlDocument.SelectSingleNode("//temp:su",xmlNamespaceManager);
xmlNode.InnerText = "myUserName";
xmlDocument.Save(@"C:\Users\D\desktop\myrequest.xml");
Console.WriteLine(xmlNode.InnerText);
执行此代码后,“ myUserName”将被写入文件中,而不是“ UserName”。您可以对“密码”和“数字”值进行相同的操作