Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# code to create this XML in XmlDocument

Posted on 2010-09-24
6
Medium Priority
?
3,978 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

 
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

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.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

662 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