Martin
Martin

Reputation: 41

Build SOAP envelope with XDocument instead of string

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.

Upvotes: 2

Views: 9141

Answers (2)

pat8719
pat8719

Reputation: 1700

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

Upvotes: 1

nlawalker
nlawalker

Reputation: 6514

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.

Upvotes: 0

Related Questions