C# 使用C以编程方式更改SOAP消息内容#

C# 使用C以编程方式更改SOAP消息内容#,c#,soap,C#,Soap,我在myrequest.xml文件中有soap请求消息,如下所示: <?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

我在myrequest.xml文件中有soap请求消息,如下所示:

<?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>

在这种情况下,您不需要添加“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”。您可以对密码和数字值执行相同的操作

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);