Problem with namespaces using Linq for XML to create xml response

I am new to L2XML and not an XML expert, so it comes as no surprise that I have small problems. In the first step, I declare a relatively simple XDocument object to create the result of the XML method.

Here is an example of the expected XML.

<?xml version="1.0" encoding="UTF-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/03/addressing" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"> <soap:Body> <TXLife xmlns="http://ACORD.org/Standards/Life/2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ACORD.org/Standards/Life/2 TXLife2.28enum.XSD"> <UserAuthResponse> <TransResult> <ResultCode tc="1">Success</ResultCode> </TransResult> <SvrDate>2010-12-02</SvrDate> <SvrTime>14:40:50-06:00</SvrTime> </UserAuthResponse> <TXLifeResponse> <TransRefGUID>V7504892456123812</TransRefGUID> <TransType tc="121">General Requirement Order Request</TransType> <TransExeDate>2010-12-02</TransExeDate> <TransExeTime>14:40:50-06:00</TransExeTime> <TransMode tc="2">Original</TransMode> <TestIndicator tc="1">Yes</TestIndicator> <TransResult> <ResultCode tc="1">Success</ResultCode> </TransResult> <OLifE> <SourceInfo> <CreationDate>2010-12-02</CreationDate> <CreationTime>14:40:50-06:00</CreationTime> <SourceInfoName>External Vendor Name</SourceInfoName> </SourceInfo> </OLifE> </TXLifeResponse> </TXLife> </soap:Body> </soap:Envelope> 

Here is the code that I use to try to create something corresponding above:

 public string SubmitOrder121(string xmlIn) { string resultText = "SUCCESS"; //Hard coded for now. Needs to be set based on result of call to CrossBow. string resultCode = "1"; //Same comment as above. string date = DateTime.Today.ToShortDateString(); string time = DateTime.Now.ToShortTimeString(); string transRefGUID = "V7504892456123812"; //Hard coded for now. Get from xmlIn; string transModeText = "Original"; //Don't know what this is for or where to get it if there are other possibilities string transModeCode = "2"; //Same as above comment string testIndicatorText = "True"; //Get from config file string testIndicatorCode = "1"; //Get from config file string companyName = "External Vendor Name"; //Get from config file XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance"; XNamespace soap = "http://www.w3.org/2001/12/soap-envelope"; XNamespace xmlns = "http://ACORD.org/Standards/Life/2"; XDocument xdoc = new XDocument( new XDeclaration("1.0", "utf-8", ""), new XElement(soap + "Envelope", new XAttribute(XNamespace.Xmlns + "wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"), new XAttribute(XNamespace.Xmlns + "wsa", "http://schemas.xmlsoap.org/ws/2004/03/addressing"), new XAttribute(XNamespace.Xmlns + "wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"), new XAttribute(XNamespace.Xmlns + "soap", "http://schemas.xmlsoap.org/soap/envelope/"), new XElement(soap + "Body", new XElement(xmlns + "TXLife", new XAttribute(xsi + "schemaLocation", "http://ACORD.org/Standards"), new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"), new XElement("UserAuthResponse", new XElement("TransResult", new XElement("ResultCode", resultText, new XAttribute("tc", resultCode) ) ), new XElement("SvrDate", date), new XElement("SvrTime", time) ), new XElement("TXLifeResponse", new XElement("TransRefGUID", transRefGUID), new XElement("TransType", "General Requiremeent Order Request", new XAttribute("tc", "121") ), new XElement("TransExeDate", date), //Get from crossbow result new XElement("TransExeTime", time), //Get from crossbow result new XElement("TransMode", transModeText, new XAttribute("tc", transModeCode) ), new XElement("TestIndicator", testIndicatorText, new XAttribute("tc", testIndicatorCode) ), new XElement("TransResult", new XElement("ResultCode", resultText, new XAttribute("tc", resultCode) ) ), new XElement("OLife", new XElement("SourceInfo", new XElement("CreationDate", date), new XElement("CreationTime", time), new XElement("SourceInfoName", companyName) ) ) ) ) ) ) ); return xdoc.ToString(); } 

Now, given the little that I have been able to understand so far, the above should give me what I want, but it is not. This gives me the following:

 <Envelope xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/03/addressing" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://www.w3.org/2001/12/soap-envelope"> <Body> <TXLife xsi:schemaLocation="http://ACORD.org/Standards" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://ACORD.org/Standards/Life/2"> <UserAuthResponse xmlns=""> <TransResult> <ResultCode tc="1">SUCCESS</ResultCode> </TransResult> <SvrDate>6/14/2013</SvrDate> <SvrTime>1:57 PM</SvrTime> </UserAuthResponse> <TXLifeResponse xmlns=""> <TransRefGUID>V7504892456123812</TransRefGUID> <TransType tc="121">General Requiremeent Order Request</TransType> <TransExeDate>6/14/2013</TransExeDate> <TransExeTime>1:57 PM</TransExeTime> <TransMode tc="2">Original</TransMode> <TestIndicator tc="1">True</TestIndicator> <TransResult> <ResultCode tc="1">SUCCESS</ResultCode> </TransResult> <OLife> <SourceInfo> <CreationDate>6/14/2013</CreationDate> <CreationTime>1:57 PM</CreationTime> <SourceInfoName>The Company Name</SourceInfoName> </SourceInfo> </OLife> </TXLifeResponse> </TXLife> </Body> </Envelope> 

Ignore date and time format. I know that they do not match, but this is what I will deal with later, as well as hard values. I'm more interested in the XML format, especially with the following:

  • Why does XTeclaration not appear as a result of XML?
  • On the muscle envelope and body, why is there no soap prefix?
  • How to suppress xmlns attribute in tags and TXLifeResponse>?

I already saw the links in the answers to other similar questions related to this link: http://msdn.microsoft.com/en-us/library/bb387042.aspx but this did not help me much.

+4
source share
1 answer
  • The ToString () method never emits XML declarations. The reason another story happens, but check with xdoc.Save ("sample.xml"); which records the declaration.

  • On the line XNamespace soap = "http://www.w3.org/2001/12/soap-envelope"; you probably have a typo, change it to http://schemas.xmlsoap.org/soap/envelope/

  • You need to specify a default namespace for all children of the TXLife element, for example:

     new XElement(xmlns + "UserAuthResponse", new XElement(xmlns + "TransResult", new XElement(xmlns + "ResultCode", resultText, new XAttribute("tc", resultCode) 

Hope this helps

+1
source

Source: https://habr.com/ru/post/1486311/


All Articles