Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

C# code to create this XML in XmlDocument

Posted on 2010-09-24
6
Medium Priority
?
4,255 Views
Last Modified: 2012-05-10
I am working on a Webservice project in ASP.Net 3.5 / C#

I want to create an XML in XmlDocument using C#. I have tried and achieved 95% !!

Only a smail issue remains.
- When I try create a node. for eg:- 'soapenv:Header', resulted XML will contain only 'Header'. It somehow skips the first part ''soapenv'. I am sure I am making a silly mistake in coding as I am not aware.
There are many similar nodes in my XML that have colon (:) which needs this correction.

Please check the attached files and please suggest me what I have to correct in my C# coding to achieve this ?

Attachments:-
1. correctXML.xml - This is what I want to create.
correctXML.XML

2. wrongXML.xml - This is what I currently created by my C# code.
wrongXML.xml

3.  current C# code
public XmlDocument createUserXMLDocument2()
    {
        XmlDocument xmlDoc = new XmlDocument();

        // XML Declaration
        XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);

        // Create the root element soapenv:Envelope 
        XmlElement rootEnvelope = xmlDoc.CreateElement("Envelope");
        rootEnvelope.SetAttribute("xmlns:soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
        rootEnvelope.SetAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
        rootEnvelope.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
        rootEnvelope.SetAttribute("xmlns:wksp", "http://schemas.ourwebservice.com/message/");
        xmlDoc.AppendChild(rootEnvelope);

        #region soapenv:Header
        // Create soapenv:Header 
        XmlElement headerNode = xmlDoc.CreateElement("Header");

        // Create soapenv:Header -> wksp:Authentication 
        XmlElement authenticationNode = xmlDoc.CreateElement("Authentication");
        authenticationNode.SetAttribute("xmlns", "");
        headerNode.AppendChild(authenticationNode);

        // Create wksp:Authentication -> username
        XmlElement usernameNode = xmlDoc.CreateElement("username");
        usernameNode.AppendChild(xmlDoc.CreateTextNode("user1"));

        // Create wksp:Authentication -> password
        XmlElement passwordNode = xmlDoc.CreateElement("password");
        passwordNode.AppendChild(xmlDoc.CreateTextNode("password123"));

        authenticationNode.AppendChild(usernameNode);
        authenticationNode.AppendChild(passwordNode);

        // Append soapenv:Header 
        rootEnvelope.AppendChild(headerNode);
        #endregion

        #region soapenv:Body
        XmlElement bodyNode = xmlDoc.CreateElement("Body");

        #region wksp:createUser

        XmlElement createuserNode = xmlDoc.CreateElement("createUser");

        // <transactionID>1201</transactionID> 
        XmlElement transactionidNode = xmlDoc.CreateElement("transactionID");
        transactionidNode.AppendChild(xmlDoc.CreateTextNode("1111"));
        createuserNode.AppendChild(transactionidNode);

        // <externalEntityID>5101</externalEntityID>
        XmlElement externalEntityIDNode = xmlDoc.CreateElement("externalEntityID");
        externalEntityIDNode.AppendChild(xmlDoc.CreateTextNode("2222"));
        createuserNode.AppendChild(externalEntityIDNode);

        // <externalUserID>1202</externalUserID>
        XmlElement externalUserIDNode = xmlDoc.CreateElement("externalUserID");
        externalUserIDNode.AppendChild(xmlDoc.CreateTextNode("3333"));
        createuserNode.AppendChild(externalUserIDNode);

        #region UserDetail

        XmlElement userdetailNode = xmlDoc.CreateElement("UserDetail");

        // <FirstName>Jimmy</FirstName> 
        XmlElement FirstNameNode = xmlDoc.CreateElement("FirstName");
        FirstNameNode.AppendChild(xmlDoc.CreateTextNode("John"));
        userdetailNode.AppendChild(FirstNameNode);

        // <LastName>Dean</LastName> 
        XmlElement LastNameNode = xmlDoc.CreateElement("LastName");
        LastNameNode.AppendChild(xmlDoc.CreateTextNode("J"));
        userdetailNode.AppendChild(LastNameNode);

        //  <LoginName>rohanpm51@workspeed.com</LoginName>
        XmlElement LoginNameNode = xmlDoc.CreateElement("LoginName");
        LoginNameNode.AppendChild(xmlDoc.CreateTextNode("user@email.com"));
        userdetailNode.AppendChild(LoginNameNode);

        //  <Password>rohanpm51</Password> 
        XmlElement PasswordNode = xmlDoc.CreateElement("Password");
        PasswordNode.AppendChild(xmlDoc.CreateTextNode("john123"));
        userdetailNode.AppendChild(PasswordNode);

        //  <EmailAddress>rohanpm51@workspeed.com</EmailAddress>
        XmlElement EmailAddressNode = xmlDoc.CreateElement("EmailAddress");
        EmailAddressNode.AppendChild(xmlDoc.CreateTextNode("user@email.com"));
        userdetailNode.AppendChild(EmailAddressNode);

        //  <HomePhone /> 
        XmlElement HomePhoneNode = xmlDoc.CreateElement("HomePhone");
        HomePhoneNode.AppendChild(xmlDoc.CreateTextNode(""));
        userdetailNode.AppendChild(HomePhoneNode);

        //  <OfficePhone>(212)400-9030</OfficePhone> 
        XmlElement OfficePhoneNode = xmlDoc.CreateElement("OfficePhone");
        OfficePhoneNode.AppendChild(xmlDoc.CreateTextNode("(222)222-222"));
        userdetailNode.AppendChild(OfficePhoneNode);

        //  <MobilePhone /> 
        XmlElement MobilePhoneNode = xmlDoc.CreateElement("MobilePhone");
        MobilePhoneNode.AppendChild(xmlDoc.CreateTextNode(""));
        userdetailNode.AppendChild(MobilePhoneNode);

        //  <WirelessNumber /> 
        XmlElement WirelessNumberNode = xmlDoc.CreateElement("WirelessNumber");
        WirelessNumberNode.AppendChild(xmlDoc.CreateTextNode(""));
        userdetailNode.AppendChild(WirelessNumberNode);

        //  <WirelessCarrierName>Not Provided</WirelessCarrierName> 
        XmlElement WirelessCarrierNameNode = xmlDoc.CreateElement("WirelessCarrierName");
        WirelessCarrierNameNode.AppendChild(xmlDoc.CreateTextNode("Not Provided"));
        userdetailNode.AppendChild(WirelessCarrierNameNode);

        #region MainAddress
        XmlElement MainAddressNode = xmlDoc.CreateElement("MainAddress");

        // <Address1>510 Madison Avenue</Address1>
        XmlElement Address1Node = xmlDoc.CreateElement("Address1");
        Address1Node.AppendChild(xmlDoc.CreateTextNode("Our Address1"));
        MainAddressNode.AppendChild(Address1Node);

        // <Address2 /> 
        XmlElement Address2Node = xmlDoc.CreateElement("Address2");
        Address2Node.AppendChild(xmlDoc.CreateTextNode(""));
        MainAddressNode.AppendChild(Address2Node);

        //  <City>New York</City> 
        XmlElement CityNode = xmlDoc.CreateElement("City");
        CityNode.AppendChild(xmlDoc.CreateTextNode("Our City"));
        MainAddressNode.AppendChild(CityNode);

        //  <Zip>10017</Zip> 
        XmlElement ZipNode = xmlDoc.CreateElement("Zip");
        ZipNode.AppendChild(xmlDoc.CreateTextNode("11111"));
        MainAddressNode.AppendChild(ZipNode);

        //  <State>NY</State> 
        XmlElement StateNode = xmlDoc.CreateElement("State");
        StateNode.AppendChild(xmlDoc.CreateTextNode("ST"));
        MainAddressNode.AppendChild(StateNode);

        //  <Country>United States</Country> 
        XmlElement CountryNode = xmlDoc.CreateElement("Country");
        CountryNode.AppendChild(xmlDoc.CreateTextNode("Our Country"));
        MainAddressNode.AppendChild(CountryNode);

        //  <EmailAddress>rohanpm51@workspeed.com</EmailAddress>
        XmlElement MAEmailAddressNode = xmlDoc.CreateElement("EmailAddress");
        MAEmailAddressNode.AppendChild(xmlDoc.CreateTextNode("user@email.com"));
        MainAddressNode.AppendChild(MAEmailAddressNode);

        //  <OfficePhone>(212)400-9029</OfficePhone> 
        XmlElement MAOfficePhoneNode = xmlDoc.CreateElement("OfficePhone");
        MAOfficePhoneNode.AppendChild(xmlDoc.CreateTextNode("(222)222-2222"));
        MainAddressNode.AppendChild(MAOfficePhoneNode);

        //  <Fax /> 
        XmlElement FaxNode = xmlDoc.CreateElement("Fax");
        FaxNode.AppendChild(xmlDoc.CreateTextNode(""));
        MainAddressNode.AppendChild(FaxNode);

        userdetailNode.AppendChild(MainAddressNode);
        #endregion

        createuserNode.AppendChild(userdetailNode);

        #endregion

        #region UserRole
        XmlElement UserRoleNode = xmlDoc.CreateElement("UserRole");

        // <externalPropertyID>684159968</externalPropertyID> 
        XmlElement externalPropertyIDNode = xmlDoc.CreateElement("externalPropertyID");
        externalPropertyIDNode.AppendChild(xmlDoc.CreateTextNode("12345"));
        UserRoleNode.AppendChild(externalPropertyIDNode);

        // <userRoleType>TENANT3</userRoleType>
        XmlElement userRoleTypeNode = xmlDoc.CreateElement("userRoleType");
        userRoleTypeNode.AppendChild(xmlDoc.CreateTextNode("Role123"));
        UserRoleNode.AppendChild(userRoleTypeNode);

        createuserNode.AppendChild(UserRoleNode);
        #endregion

        bodyNode.AppendChild(createuserNode);
        rootEnvelope.AppendChild(bodyNode);
        #endregion

        #endregion

        //xmlDoc.Save("C:\\wrongXML.xml");
        return xmlDoc;
    }

Open in new window

 


I am sure this is silly thing for who knows XML coding!

Thanks in Advance
Raj
0
Comment
Question by:Rajkumar Gs
  • 3
  • 2
6 Comments
 
LVL 23

Author Comment

by:Rajkumar Gs
ID: 33753850
Tried this code
xmlDoc.CreateElement("soapenv","Envelope");
to create XML node
<soapenv:Envelope ...

Not working. Any idea ?

Raj
0
 
LVL 20

Assisted Solution

by:Daniel Van Der Werken
Daniel Van Der Werken earned 500 total points
ID: 33753954
XmlElement headerNode = xmlDoc.CreateElement( "soapenv", "Header", "http://schemas.xmlsoap.org/soap/envelope/" );

works.
0
 
LVL 23

Author Comment

by:Rajkumar Gs
ID: 33754196
It works for these nodes
soapenv:Envelope   &
soapenv:Header
I think since both these nodes have common first part 'soapenv'

But in case of the nodes that having different first part (they have 'wksp'), resulted XML is displaying wrong.
xmlns:wksp="http://schemas.xmlsoap.org/soap/envelope/" is getting displayed as the attribute of that nodes.

Please check the screenshots for more clear picture

Thanks
Raj
correctXML.bmp
wrongXML.bmp
0
Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1500 total points
ID: 33756357
You'll need to set each node appropriately. I modified your original code a touch. This should give the output you are looking for:
public static XmlDocument createUserXMLDocument2()
{
    const string SOAPENV_NS = "http://schemas.xmlsoap.org/soap/envelope/";
    const string WKSP_NS = "http://schemas.ourwebservice.com/message/";

    XmlDocument xmlDoc = new XmlDocument();

    // XML Declaration
    XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);

    // Create the root element soapenv:Envelope 
    XmlElement rootEnvelope = xmlDoc.CreateElement("soapenv", "Envelope", SOAPENV_NS);
    rootEnvelope.SetAttribute("xmlns:soapenv", SOAPENV_NS);
    rootEnvelope.SetAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
    rootEnvelope.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
    rootEnvelope.SetAttribute("xmlns:wksp", WKSP_NS);
    xmlDoc.AppendChild(rootEnvelope);

    #region soapenv:Header
    // Create soapenv:Header 
    XmlElement headerNode = xmlDoc.CreateElement("soapenv", "Header", SOAPENV_NS);

    // Create soapenv:Header -> wksp:Authentication 
    XmlElement authenticationNode = xmlDoc.CreateElement("wksp", "Authentication", WKSP_NS);
    authenticationNode.SetAttribute("xmlns", "");
    headerNode.AppendChild(authenticationNode);

    // Create wksp:Authentication -> username
    XmlElement usernameNode = xmlDoc.CreateElement("username");
    usernameNode.AppendChild(xmlDoc.CreateTextNode("user1"));

    // Create wksp:Authentication -> password
    XmlElement passwordNode = xmlDoc.CreateElement("password");
    passwordNode.AppendChild(xmlDoc.CreateTextNode("password123"));

    authenticationNode.AppendChild(usernameNode);
    authenticationNode.AppendChild(passwordNode);

    // Append soapenv:Header 
    rootEnvelope.AppendChild(headerNode);
    #endregion

    #region soapenv:Body
    XmlElement bodyNode = xmlDoc.CreateElement("soapenv", "Body", SOAPENV_NS);

    #region wksp:createUser

    XmlElement createuserNode = xmlDoc.CreateElement("wksp", "createUser", WKSP_NS);

    // <transactionID>1201</transactionID> 
    XmlElement transactionidNode = xmlDoc.CreateElement("transactionID");
    transactionidNode.AppendChild(xmlDoc.CreateTextNode("1111"));
    createuserNode.AppendChild(transactionidNode);

    // <externalEntityID>5101</externalEntityID>
    XmlElement externalEntityIDNode = xmlDoc.CreateElement("externalEntityID");
    externalEntityIDNode.AppendChild(xmlDoc.CreateTextNode("2222"));
    createuserNode.AppendChild(externalEntityIDNode);

    // <externalUserID>1202</externalUserID>
    XmlElement externalUserIDNode = xmlDoc.CreateElement("externalUserID");
    externalUserIDNode.AppendChild(xmlDoc.CreateTextNode("3333"));
    createuserNode.AppendChild(externalUserIDNode);

    #region UserDetail

    XmlElement userdetailNode = xmlDoc.CreateElement("UserDetail");

    // <FirstName>Jimmy</FirstName> 
    XmlElement FirstNameNode = xmlDoc.CreateElement("FirstName");
    FirstNameNode.AppendChild(xmlDoc.CreateTextNode("John"));
    userdetailNode.AppendChild(FirstNameNode);

    // <LastName>Dean</LastName> 
    XmlElement LastNameNode = xmlDoc.CreateElement("LastName");
    LastNameNode.AppendChild(xmlDoc.CreateTextNode("J"));
    userdetailNode.AppendChild(LastNameNode);

    //  <LoginName>rohanpm51@workspeed.com</LoginName>
    XmlElement LoginNameNode = xmlDoc.CreateElement("LoginName");
    LoginNameNode.AppendChild(xmlDoc.CreateTextNode("user@email.com"));
    userdetailNode.AppendChild(LoginNameNode);

    //  <Password>rohanpm51</Password> 
    XmlElement PasswordNode = xmlDoc.CreateElement("Password");
    PasswordNode.AppendChild(xmlDoc.CreateTextNode("john123"));
    userdetailNode.AppendChild(PasswordNode);

    //  <EmailAddress>rohanpm51@workspeed.com</EmailAddress>
    XmlElement EmailAddressNode = xmlDoc.CreateElement("EmailAddress");
    EmailAddressNode.AppendChild(xmlDoc.CreateTextNode("user@email.com"));
    userdetailNode.AppendChild(EmailAddressNode);

    //  <HomePhone /> 
    XmlElement HomePhoneNode = xmlDoc.CreateElement("HomePhone");
    HomePhoneNode.AppendChild(xmlDoc.CreateTextNode(""));
    userdetailNode.AppendChild(HomePhoneNode);

    //  <OfficePhone>(212)400-9030</OfficePhone> 
    XmlElement OfficePhoneNode = xmlDoc.CreateElement("OfficePhone");
    OfficePhoneNode.AppendChild(xmlDoc.CreateTextNode("(222)222-222"));
    userdetailNode.AppendChild(OfficePhoneNode);

    //  <MobilePhone /> 
    XmlElement MobilePhoneNode = xmlDoc.CreateElement("MobilePhone");
    MobilePhoneNode.AppendChild(xmlDoc.CreateTextNode(""));
    userdetailNode.AppendChild(MobilePhoneNode);

    //  <WirelessNumber /> 
    XmlElement WirelessNumberNode = xmlDoc.CreateElement("WirelessNumber");
    WirelessNumberNode.AppendChild(xmlDoc.CreateTextNode(""));
    userdetailNode.AppendChild(WirelessNumberNode);

    //  <WirelessCarrierName>Not Provided</WirelessCarrierName> 
    XmlElement WirelessCarrierNameNode = xmlDoc.CreateElement("WirelessCarrierName");
    WirelessCarrierNameNode.AppendChild(xmlDoc.CreateTextNode("Not Provided"));
    userdetailNode.AppendChild(WirelessCarrierNameNode);

    #region MainAddress
    XmlElement MainAddressNode = xmlDoc.CreateElement("MainAddress");

    // <Address1>510 Madison Avenue</Address1>
    XmlElement Address1Node = xmlDoc.CreateElement("Address1");
    Address1Node.AppendChild(xmlDoc.CreateTextNode("Our Address1"));
    MainAddressNode.AppendChild(Address1Node);

    // <Address2 /> 
    XmlElement Address2Node = xmlDoc.CreateElement("Address2");
    Address2Node.AppendChild(xmlDoc.CreateTextNode(""));
    MainAddressNode.AppendChild(Address2Node);

    //  <City>New York</City> 
    XmlElement CityNode = xmlDoc.CreateElement("City");
    CityNode.AppendChild(xmlDoc.CreateTextNode("Our City"));
    MainAddressNode.AppendChild(CityNode);

    //  <Zip>10017</Zip> 
    XmlElement ZipNode = xmlDoc.CreateElement("Zip");
    ZipNode.AppendChild(xmlDoc.CreateTextNode("11111"));
    MainAddressNode.AppendChild(ZipNode);

    //  <State>NY</State> 
    XmlElement StateNode = xmlDoc.CreateElement("State");
    StateNode.AppendChild(xmlDoc.CreateTextNode("ST"));
    MainAddressNode.AppendChild(StateNode);

    //  <Country>United States</Country> 
    XmlElement CountryNode = xmlDoc.CreateElement("Country");
    CountryNode.AppendChild(xmlDoc.CreateTextNode("Our Country"));
    MainAddressNode.AppendChild(CountryNode);

    //  <EmailAddress>rohanpm51@workspeed.com</EmailAddress>
    XmlElement MAEmailAddressNode = xmlDoc.CreateElement("EmailAddress");
    MAEmailAddressNode.AppendChild(xmlDoc.CreateTextNode("user@email.com"));
    MainAddressNode.AppendChild(MAEmailAddressNode);

    //  <OfficePhone>(212)400-9029</OfficePhone> 
    XmlElement MAOfficePhoneNode = xmlDoc.CreateElement("OfficePhone");
    MAOfficePhoneNode.AppendChild(xmlDoc.CreateTextNode("(222)222-2222"));
    MainAddressNode.AppendChild(MAOfficePhoneNode);

    //  <Fax /> 
    XmlElement FaxNode = xmlDoc.CreateElement("Fax");
    FaxNode.AppendChild(xmlDoc.CreateTextNode(""));
    MainAddressNode.AppendChild(FaxNode);

    userdetailNode.AppendChild(MainAddressNode);
    #endregion

    createuserNode.AppendChild(userdetailNode);

    #endregion

    #region UserRole
    XmlElement UserRoleNode = xmlDoc.CreateElement("UserRole");

    // <externalPropertyID>684159968</externalPropertyID> 
    XmlElement externalPropertyIDNode = xmlDoc.CreateElement("externalPropertyID");
    externalPropertyIDNode.AppendChild(xmlDoc.CreateTextNode("12345"));
    UserRoleNode.AppendChild(externalPropertyIDNode);

    // <userRoleType>TENANT3</userRoleType>
    XmlElement userRoleTypeNode = xmlDoc.CreateElement("userRoleType");
    userRoleTypeNode.AppendChild(xmlDoc.CreateTextNode("Role123"));
    UserRoleNode.AppendChild(userRoleTypeNode);

    createuserNode.AppendChild(UserRoleNode);
    #endregion

    bodyNode.AppendChild(createuserNode);
    rootEnvelope.AppendChild(bodyNode);
    #endregion

    #endregion

    //xmlDoc.Save("C:\\wrongXML.xml");
    return xmlDoc;
}

Open in new window

0
 
LVL 23

Author Closing Comment

by:Rajkumar Gs
ID: 33759231
@kaufmed,
PERFECT!!  Thanks great for your COMPLETE SOLUTION!!
Your code is seeems to be self descriptive. So I got the logic behind creating the XML and how you fixed it!!
Now I learned to create any XML in C# :-)

@Dan7el, Thank you for quick reply which was half done.

Raj
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33760430
NP. Glad to help :)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Integration Management Part 2
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question