目前我有这个代码来构建一个肥皂信封:
"<?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)))));
我该如何完成?
提前致谢。
答案 0 :(得分:1)
您可以使用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);
答案 1 :(得分:0)
这涵盖了所有细节:http://msdn.microsoft.com/en-us/library/bb387042.aspx
您不能像想象字符串连接代码一样考虑DOM API(如L2XML或XmlDocument)。使用L2XML时,名称空间声明和属性是需要明确处理的“事物”,而不仅仅是尖括号之间的额外字符。因此,XElement构造函数不像“这是我想要在尖括号之间的字符串”那么简单。相反,您需要使用处理命名空间的备用XElement构造函数。