gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 14:18:04 25 4
gpt4 key购买 nike

目前我有这段代码来构建 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> " +
"<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)))));

我怎样才能完成这个?

提前致谢。

最佳答案

您可以使用 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);

关于c# - 使用 XDocument 而不是字符串构建 SOAP 信封,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6112281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com