使用 XDocument 而不是字符串构建 SOAP 信封

发布于 2024-11-09 14:00:58 字数 1727 浏览 0 评论 0原文

目前我有这段代码来构建一个肥皂信封:

     "<?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> " +
     "<ABRSearchByABN xmlns=\"http://abr.business.gov.au/ABRXMLSearch/\"> " +
     "<searchString>" + searchValue + "</searchString>" +
     "<includeHistoricalDetails>" + history + "</includeHistoricalDetails>" +
     "<authenticationGuid>" + guid + "</authenticationGuid>" +
     "</ABRSearchByABN>" +
     "</soap:Body>" +
     "</soap:Envelope>";

我正在尝试创建一个 XML 文档,但我不确定如何继续使用名称空间。

显然不起作用的代码:

XNamespace soap = "http://schemas.xmlsoap.org/soap/envelope/";
XNamespace xmlns = "http://abr.business.gov.au/ABRXMLSearch/";
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XNamespace xsd = "http://www.w3.org/2001/XMLSchema";

XDocument xd = new XDocument(
    new XDeclaration("1.0","utf-8",""),
      new XElement("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/\"",   
          new XElement("soap:Body",
              new XElement("ABRSearchByABN xmlns=\"http://abr.business.gov.au/ABRXMLSearch/\"",
                  new XElement("searchString", searchValue),
                  new XElement("includeHistoricalDetails", history),
                  new XElement("authenticationGuid", guid)))));

我怎样才能完成这个?

提前致谢。

currently I have this code to build a soap envelope:

     "<?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> " +
     "<ABRSearchByABN xmlns=\"http://abr.business.gov.au/ABRXMLSearch/\"> " +
     "<searchString>" + searchValue + "</searchString>" +
     "<includeHistoricalDetails>" + history + "</includeHistoricalDetails>" +
     "<authenticationGuid>" + guid + "</authenticationGuid>" +
     "</ABRSearchByABN>" +
     "</soap:Body>" +
     "</soap:Envelope>";

I am trying to create an XML document instead but I am not sure on how to go on with the namespaces.

Code that obiously doesn't work:

XNamespace soap = "http://schemas.xmlsoap.org/soap/envelope/";
XNamespace xmlns = "http://abr.business.gov.au/ABRXMLSearch/";
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XNamespace xsd = "http://www.w3.org/2001/XMLSchema";

XDocument xd = new XDocument(
    new XDeclaration("1.0","utf-8",""),
      new XElement("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/\"",   
          new XElement("soap:Body",
              new XElement("ABRSearchByABN xmlns=\"http://abr.business.gov.au/ABRXMLSearch/\"",
                  new XElement("searchString", searchValue),
                  new XElement("includeHistoricalDetails", history),
                  new XElement("authenticationGuid", guid)))));

How can I complete this?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那一片橙海, 2024-11-16 14:00:58

您可以使用 XmlDocument 类并继续追加子级。

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.AppendChild(xmlDoc.CreateNode(XmlNodeType.Element, "soap", "Envelope", "http://www.w3.org/2001/XMLSchema-instance"));

另外,假设您已经将 xml 存储为字符串,一个更简单但可能不太易于管理的解决方案是简单地声明一个新的 XmlDocument 并将字符串加载到其中。

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(yourString);  

You could use the XmlDocument class and continue to append children.

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.AppendChild(xmlDoc.CreateNode(XmlNodeType.Element, "soap", "Envelope", "http://www.w3.org/2001/XMLSchema-instance"));

Also, assuming you already have your xml stored as a string, an even simpler although potentially less manageable solution would be to simply declare a new XmlDocument and load the string to it.

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(yourString);  
微暖i 2024-11-16 14:00:58

这涵盖了非常详细的所有内容: http://msdn.microsoft.com/en -us/library/bb387042.aspx

您不能像考虑字符串连接代码那样考虑 DOM API(如 L2XML 或 XmlDocument)。使用 L2XML 时,名称空间声明和属性是需要显式处理的“事物”,而不仅仅是尖括号之间的额外字符。因此,XElement 构造函数并不像“这是我想要放在尖括号之间的字符串”那么简单。相反,您需要使用处理命名空间的备用 XElement 构造函数。

This covers everything in pretty good detail: http://msdn.microsoft.com/en-us/library/bb387042.aspx

You can't think of a DOM API (like L2XML or XmlDocument) the same way as you think of your string-concatenating code. When working with L2XML, namespace declarations and attributes are "things" that need to be handled explicitly, not just extra characters that go between angle brackets. Consequently, the XElement constructor isn't as simple as "here is the string I want to go between the angle brackets." Instead, you need to use an alternate XElement constructor that handles namespaces.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文