Link to home
Start Free TrialLog in
Avatar of Rajkumar Gs
Rajkumar GsFlag for India

asked on

C# code to create this XML in XmlDocument

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
Avatar of Rajkumar Gs
Rajkumar Gs
Flag of India image

ASKER

Tried this code
xmlDoc.CreateElement("soapenv","Envelope");
to create XML node
<soapenv:Envelope ...

Not working. Any idea ?

Raj
SOLUTION
Avatar of Daniel Van Der Werken
Daniel Van Der Werken
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@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
NP. Glad to help :)