Solved

C# code to create this XML in XmlDocument

Posted on 2010-09-24
6
3,107 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 125 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 375 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with solving error related to resources file 4 36
SQL XML ALL Nodes Compare in function 2 18
Connection String 16 43
Entity Framework 7 32
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now