Search code examples
c#asp.netxmlxmldocument

Creating XMLDocument throguh code in asp.net


am trying to generate an XML document like this through code.

<TestRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://localhost:2292/RMSchema.xsd">
    <Version>3</Version>
    <ApplicationHeader>
        <AppLanguage />
        <UserId>rmservice</UserId>
    </ApplicationHeader>
    <CustomerData>
        <ExistingCustomerData>
            <MTN>2084127182</MTN>
        </ExistingCustomerData>
    </CustomerData>
</TestRequest>

I tried some samples. But they create xmlns for the children, which i dont need. Any help is really appreciated.

I have tried the below code. But it is adding only xmlns to all children, which i dont need

XmlDocument xDocument = new XmlDocument();
xDocument.AppendChild(xDocument.CreateXmlDeclaration("1.0", "windows-1252", null));
XmlElement xRoot = xDocument.CreateElement("TestRequest", "XNamespace.Xmlns=http://www.w3.org/2001/XMLSchema-instance" + " xsi:noNamespaceSchemaLocation=" + "http://localhost:2292/RMSchema.xsd");
xDocument.AppendChild(xRoot);
xRoot.AppendChild(xDocument.CreateElement("Version")).InnerText = 1;

Thanks Tutu

I have tried with

var xsi = "http://www.w3.org/2001/XMLSchema-instance";
            XmlElement xRoot = xDocument.CreateElement("xsi","RMRequest",xsi);
            xRoot.SetAttribute("noNamespaceSchemaLocation", xsi, "http://localhost:2292/RMSchema.xsd");

            xDocument.AppendChild(xRoot);
Now the response is 

<?xml version=\"1.0\" encoding=\"windows-1252\"?><xsi:TestRequest xsi:noNamespaceSchemaLocation=\"http://localhost:2292/RMSchema.xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">

Solution

  • Here is the awesome LINQ to XML. Enjoy!

    XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
    XDocument doc = new XDocument(new XDeclaration("1.0", "windows-1252", null),  
        new XElement("TestRequest",
            new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
            new XAttribute(xsi + "noNamespaceSchemaLocation", "http://localhost:2292/RMSchema.xsd"),
            new XElement("Version",
                    new XText("3")
            ),
            new XElement("ApplicationHeader",
                    new XElement("AppLanguage"),
                    new XElement("UserId",
                            new XText("rmservice")
                    )
            ),
            new XElement("CustomerData",
                new XElement("ExistingCustomerData",
                    new XElement("MTN", 
                        new XText("2084127182")
                    )
                )
            )
        )
    );
    
    doc.Save(filePath);
    

    If you really want the old API, here it is:

    var xDocument = new XmlDocument();
    xDocument.AppendChild(xDocument.CreateXmlDeclaration("1.0", "windows-1252", null));
    
    var xsi = "http://www.w3.org/2001/XMLSchema-instance";
    var xRoot = xDocument.CreateElement("TestRequest");
    
    var attr = xDocument.CreateAttribute("xsi", "noNamespaceSchemaLocation", xsi);
    attr.Value = "http://localhost:2292/RMSchema.xsd";
    xRoot.Attributes.Append(attr);
    
    xRoot.AppendChild(xDocument.CreateElement("Version")).InnerText = "1";
    
    // ..  your other elemets ...
    
    xDocument.AppendChild(xRoot);
    xDocument.Save(filePath);
    

    EDIT: From your comments, it looks like you want the xmlns:xsi and other attribute in that specific order. If so, you may have to trick the XmlDocument to add the xmlns:xsi attribute first.

    var xDocument = new XmlDocument();
    xDocument.AppendChild(xDocument.CreateXmlDeclaration("1.0", "windows-1252", null));
    
    var xsi = "http://www.w3.org/2001/XMLSchema-instance";
    var xRoot = xDocument.CreateElement("TestRequest");
    
    // add namespace decl are attribute
    var attr = xDocument.CreateAttribute("xmlns:xsi");
    attr.Value = xsi;
    xRoot.Attributes.Append(attr);
    
    // no need to specify prefix, XmlDocument will figure it now
    attr = xDocument.CreateAttribute("noNamespaceSchemaLocation", xsi);
    attr.Value = "http://localhost:2292/RMSchema.xsd";
    xRoot.Attributes.Append(attr);
    
    xRoot.AppendChild(xDocument.CreateElement("Version")).InnerText = "1";
    
    // ..  your other elemets ...
    
    xDocument.AppendChild(xRoot);
    xDocument.Save(filePath);