1

I am trying to create xml file using XDcoument, but I am getting following error

Name cannot begin with the '<' character, hexadecimal value 0x3C

here is my code

XDocument d = new XDocument(
                new XElement("<S:Envelope xmlns:S='http://schemas.xmlsoap.org/soap/envelope/'>",
                    new XElement("<S:Header xmlns:wsse='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'>",
                        new XElement("<ns13:ACASecurityHeader xmlns='urn:us:gov:treasury:irs:ext:aca:air:7.0' xmlns:ns10='urn:us:gov:treasury:irs:msg:acauibusinessheader' xmlns:ns11='http://www.w3.org/2000/09/xmldsig#' xmlns:ns12='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd' xmlns:ns13='urn:us:gov:treasury:irs:msg:acasecurityheader' xmlns:ns2='urn:us:gov:treasury:irs:common' xmlns:ns3='urn:us:gov:treasury:irs:msg:form1094-1095Btransmitterupstreammessage' xmlns:ns4='urn:us:gov:treasury:irs:msg:form1094-1095Ctransmitterupstreammessage' xmlns:ns5='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd' xmlns:ns6='urn:us:gov:treasury:irs:msg:form1094-1095BCtransmittermessage' xmlns:ns7='urn:us:gov:treasury:irs:msg:form1094-1095BCtransmitterreqmessage' xmlns:ns8='urn:us:gov:treasury:irs:msg:irsacabulkrequesttransmitter' xmlns:ns9='urn:us:gov:treasury:irs:msg:acabusinessheader'>"),
                        new XElement("Author", "Moreno, Jordao")
                        ),
                        new XElement("Book",
                        new XElement("Title", "Midieval Tools and Implements"),
                        new XElement("Author", "Gazit, Inbar")
                        )
                    ),
                new XComment("This is another comment."));

Can someone please help me on this?

here is sample XML file which I want to generate using XDocumententer image description here

M005
  • 824
  • 1
  • 9
  • 25
  • 1
    Why are you trying to create a SOAP message via XDocument? – Tim Feb 02 '16 at 06:01
  • @Tim thanks for your reply but due to some constrain I am trying to use XDocument for SOAP request. – M005 Feb 02 '16 at 06:04
  • You have namespaces to contend with...trying to work up a quick sample to show you how to do this. – Tim Feb 02 '16 at 06:05
  • use < for < and > for > ..it should work – Megha Feb 02 '16 at 06:08
  • @Meghaa - Not in a call to `XElement`. – Tim Feb 02 '16 at 06:13
  • Hey, M005, we're both working on the same problem with the IRS ACA A2A interface. I'm stuck at the same place you are I think, getting gzip working. I'll let you know if I make progress or find a fix, if you wouldn't mind doing the same for me. Send me an email so we can communicate on this if you want. I'm at bon@bonfranklin.com – Bon Feb 04 '16 at 19:46
  • @Bon Thanks for your comment, I will send you an email soon about my solution. – M005 Feb 05 '16 at 05:00
  • Thanks! I actually did figure out the gzip portion though since I posted my message. http://stackoverflow.com/questions/35182951/c-irs-aca-sending-a-web-service-request-with-both-mtom-attachment-and-gzip-en/35213457#35213457 – Bon Feb 05 '16 at 16:01
  • Now I'm on the per-element x509 signing. – Bon Feb 05 '16 at 16:01

2 Answers2

3

There is a much simpler way to do this rather than crafting the XML document by hand via XDocument, though I have an explanation and example below if you want to do it that way.

First, the simple way - create the XML as a string, and pass that string to XDocument.Parse, like this:

string xmlString = @"<S:Envelope xmlns:S=""http://schemas.xmlsoap.org/soap/envelope/""><S:Header xmlns:wsse=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd""><ns13:ACASecurityHeader xmlns:ns10=""urn:us:gov:treasury:irs:msg:acauibusinessheader"" xmlns:ns11=""http://www.w3.org/2000/09/xmldsig#"" xmlns:ns12=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"" xmlns:ns13=""urn:us:gov:treasury:irs:msg:acasecurityheader"" xmlns:ns2=""urn:us:gov:treasury:irs:common"" xmlns:ns3=""urn:us:gov:treasury:irs:msg:form1094-1095Btransmitterupstreammessage"" xmlns:ns4=""urn:us:gov:treasury:irs:msg:form1094-1095Ctransmitterupstreammessage"" xmlns:ns5=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"" xmlns:ns6=""urn:us:gov:treasury:irs:msg:form1094-1095BCtransmittermessage"" xmlns:ns7=""urn:us:gov:treasury:irs:msg:form1094-1095BCtransmitterreqmessage"" xmlns:ns8=""urn:us:gov:treasury:irs:msg:irsacabulkrequesttransmitter"" xmlns:ns9=""urn:us:gov:treasury:irs:msg:acabusinessheader""><Author>Moreno, Jordao</Author><Book><Title>Midieval Tools and Implement</Title><Author>Gazit, Inbar</Author></Book></ns13:ACASecurityHeader><!--This is another comment--></S:Header></S:Envelope>";

XDocument xDoc2 = XDocument.Parse(xmlString);

xDoc2 will contain the XML you wish to send.

If you wish to do it the long way, then there are a couple of issues with your posted code.

First, you're not correctly handling the namespaces (the xmlns: attributes). Secondly, you're including the < and > in the call to XElement, and you don't need to do that - the method takes care of those two symbols.

What you need to do is to set up the namespaces, then add them to the appropriate elements as well as creating the attributes for them.

The sample code doesn't match the posted snippet, so I worked off your sample code to show you how to go about crafting the XML by hand.

XNamespace sNS = XNamespace.Get("http://schemas.xmlsoap.org/soap/envelope/");
XNamespace wsseNS = XNamespace.Get("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
XNamespace xmlnsNS = XNamespace.Get("urn:us:gov:treasury:irs:ext:aca:air:7.0");
XNamespace ns10NS = XNamespace.Get("urn:us:gov:treasury:irs:msg:acauibusinessheader");
XNamespace ns11NS = XNamespace.Get("http://www.w3.org/2000/09/xmldsig#");
XNamespace ns12NS = XNamespace.Get("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
XNamespace ns13NS = XNamespace.Get("urn:us:gov:treasury:irs:msg:acasecurityheader");
XNamespace ns2NS = XNamespace.Get("xmlns: ns2 = 'urn:us:gov:treasury:irs:common");
XNamespace ns3NS = XNamespace.Get("urn:us:gov:treasury:irs:msg:form1094-1095Btransmitterupstreammessage");
XNamespace ns4NS = XNamespace.Get("urn:us:gov:treasury:irs:msg:form1094-1095Ctransmitterupstreammessage");
XNamespace ns5NS = XNamespace.Get("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
XNamespace ns6NS = XNamespace.Get("urn:us:gov:treasury:irs:msg:form1094-1095BCtransmittermessage");
XNamespace ns7NS = XNamespace.Get("urn:us:gov:treasury:irs:msg:form1094-1095BCtransmitterreqmessage");
XNamespace ns8NS = XNamespace.Get("urn:us:gov:treasury:irs:msg:irsacabulkrequesttransmitter");
XNamespace ns9NS = XNamespace.Get("urn:us:gov:treasury:irs:msg:acabusinessheader");

XDocument xDoc = new XDocument(new XElement(sNS + "Envelope", new XAttribute(XNamespace.Xmlns + "S", sNS),
                        new XElement(sNS + "Header", new XAttribute(XNamespace.Xmlns + "wsse", wsseNS),
                            new XElement(ns13NS + "ACASecurityHeader", new XAttribute(XNamespace.Xmlns + "ns10", ns10NS),
                                new XAttribute(XNamespace.Xmlns + "ns11", ns11NS),
                                new XAttribute(XNamespace.Xmlns + "ns12", ns12NS),
                                new XAttribute(XNamespace.Xmlns + "ns13", ns13NS),
                                new XAttribute(XNamespace.Xmlns + "ns2", ns2NS),
                                new XAttribute(XNamespace.Xmlns + "ns3", ns3NS),
                                new XAttribute(XNamespace.Xmlns + "ns4", ns4NS),
                                new XAttribute(XNamespace.Xmlns + "ns5", ns5NS),
                                new XAttribute(XNamespace.Xmlns + "ns6", ns6NS),
                                new XAttribute(XNamespace.Xmlns + "ns7", ns7NS),
                                new XAttribute(XNamespace.Xmlns + "ns8", ns8NS),
                                new XAttribute(XNamespace.Xmlns + "ns9", ns9NS
                                new XAttribute("xmlns", xmlnsNS),
                                new XElement("Author", "Moreno, Jordao"),
                                new XElement("Book",
                                    new XElement("Title", "Midieval Tools and Implement"),
                                    new XElement("Author", "Gazit, Inbar"))
                                ),
                            new XComment("This is another comment")
                        ))
    );

The first thing the above code does is sets up all the namespaces via XNamespace.

Next, the XML Document is constructed. The individual elements are created via XElement, with the various namespaces prefixed (i.e., new XElement(sNS + "Envelope",, and the other namespaces added via XAttribute.

Nesting can get tricky, so you have to be very careful doing it this way. The above code will produce the following XML:

<?xml version="1.0"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
  <S:Header xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
    <ns13:ACASecurityHeader xmlns="urn:us:gov:treasury:irs:ext:aca:air:7.0"
                            xmlns:ns9="urn:us:gov:treasury:irs:msg:acabusinessheader"
                            xmlns:ns8="urn:us:gov:treasury:irs:msg:irsacabulkrequesttransmitter" 
                            xmlns:ns7="urn:us:gov:treasury:irs:msg:form1094-1095BCtransmitterreqmessage"
                            xmlns:ns6="urn:us:gov:treasury:irs:msg:form1094-1095BCtransmittermessage" 
                            xmlns:ns5="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
                            xmlns:ns4="urn:us:gov:treasury:irs:msg:form1094-1095Ctransmitterupstreammessage"
                            xmlns:ns3="urn:us:gov:treasury:irs:msg:form1094-1095Btransmitterupstreammessage"                                xmlns:ns2="urn:us:gov:treasury:irs:common" 
                            xmlns:ns13="urn:us:gov:treasury:irs:msg:acasecurityheader" 
                            xmlns:ns12="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
                            xmlns:ns11="http://www.w3.org/2000/09/xmldsig#" 
                            xmlns:ns10="urn:us:gov:treasury:irs:msg:acauibusinessheader">
      <Author>Moreno, Jordao</Author>
      <Book>
        <Title>Midieval Tools and Implement</Title>
        <Author>Gazit, Inbar</Author>
      </Book>
    </ns13:ACASecurityHeader>
    <!--This is another comment-->
  </S:Header>
</S:Envelope>
Tim
  • 28,212
  • 8
  • 63
  • 76
  • Thanks for your reply, but I want to pass dynamic value into XML that's why I choose to use XDocument. Can you please tell me Is there any possibility where I can pass dynamic value into string xml? – M005 Feb 02 '16 at 07:39
  • 1
    @M005 - you can pass any values you want into a string if you build it. It's just a matter of determining what you want to put in the string. – Tim Feb 02 '16 at 07:50
0

What you're doing is a really hard way to do this. There is a much easier way.

You have the Xsd specifications from them, you can use the xsd command in the Visual Studio command line to generate C# objects that match the requirements automatically during serialization.

For the IRS ACA schemas, get all the XSD files into the same directory. Then in a sibling directory to the one you created, place the Common folder.

Then, in the command line navigate to the directory you created and put all the xsd files and run this command:

xsd /c IRS-EXT-ACA-AIR-7.0.xsd IRS-ACABulkRequestTransmitterMessage.xsd IRS-Form1094-1095CTransmitterUpstreamMessage.xsd IRS-CAC.xsd IRS-WSTimeStampElementMessage.xsd IRS-WSTimeStampElementMessage.xsd

You'll end up with a C# file that has almost 200 objects in it including all the enums and such necessary for generating data compliant with their specifications.

Bon
  • 1,083
  • 12
  • 23