bnfdvn
bnfdvn

Reputation: 1

c# Soap in xml file

I need to create .xml files that I will store in a folder for another computer to come and pick up. I have everything figure out except how to get the following included in the file:

<soap:Envelope xlmns=(I have the namespace text figured out)>
  <soap:Body>
.
.
.
  </soap:Body>
</soap:Envelope>

How do I incorporate the Soap:Body and Soap:Envelope elements into the file??

Here is the code

  string[] source = File.ReadAllLines(sAllFileName);
  File.Delete(sAllFileName);
  //  Create message type 3
  XElement msg3 = new XElement(@"soap:Envelope",
      from str in source
      let fields = str.Split(',')
      select new XElement(@"soap:Body",
          new XElement("REPORT_INFO",
              new XElement("MESSAGE_NO", temp3),
              new XElement("MESSAGE_TYPE", "3"),
              new XElement("BATCH_NO", siDataForCSV[0]),
              new XElement("PRODUCT_DATUM",
                  new XElement("TEST_DATA",
                      new XElement("NORM_TYPE", fields[3]),
                      new XElement("SAMPLE_ID", fields[4]),
                      new XElement("RESULT_SOURCE", fields[5]),
                      new XElement("ENTERED_BY", fields[6]),
                      new XElement("REPORTED", fields[7]),
                      new XElement("RESULT", fields[8])
                      )
                  )
              )
          )
      );

The error message says "can't have a : in a name"

Upvotes: 0

Views: 1388

Answers (1)

Tim
Tim

Reputation: 28520

The soap: is a namespace prefix, but you have to handle it differently when building the XElement - you can't simply put "soap:element name" in there.

You can use XNamespace to set up the XMLNS namespace, and then prepend it to the element name. Use XAttribute to set the namespace prefix.

Try this:

XNamespace soap = "http://www.w3.org/2003/05/soap-envelope";
XElement msg3 = new XElement(soap + @"Envelope",
                    new XAttribute(XNamespace.Xmlns + "soap", "http://www.w3.org/2003/05/soap-envelope"),
                      from str in source
                      let fields = str.Split(',')
                      select new XElement(soap + @"Body",

This will give you

<soap:Envelope xmlns=http://www.w3.org/2003/05/soap-envelope>
  <soap:Body>
  </soap:Body>
</soap:Envelope>

Added

To add an XML declaration you can switch from XElement to XDocument and use the XDeclaration property:

XNamespace soap = "http://www.w3.org/2003/05/soap-envelope";
XDocument msg3 = new XDocument("1.0", "utf-8", "yes"),
                   new XElement(soap + @"Envelope",
                      new XAttribute(XNamespace.Xmlns + "soap", "http://www.w3.org/2003/05/soap-envelope"),
                        from str in source
                        let fields = str.Split(',')
                        select new XElement(soap + @"Body",

This will give you an XDocument with an XML declaration like this:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>

If you look at this in the debugger, you may not see the XML declaration but if you look at the XDeclaration property in the debugger or save it to a file (via the Save(string fileName) method you will.

Upvotes: 1

Related Questions