Solved

C# code to create this XML in XmlDocument

Posted on 2010-09-24
6
3,003 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 19

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 74

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 74

Expert Comment

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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

11 Experts available now in Live!

Get 1:1 Help Now