Solved

C# code to create this XML file

Posted on 2010-09-06
10
514 Views
Last Modified: 2012-05-10
Could you please post the C# code to create the attached XML file ?

I googled and got many tutorials, but seems to be confusing. Hope some experienced hands can write it soon.

I tried. But result is not as expected! I am sure this is not the right way (See the attached code I tried with. This is not the final code - by undo/redo lost some)

Attachments:-
1. The code I tried
2. XML file that need to be created with C# code.

Thanks
Raj
public void writeXML()
    {
        // Create a new file in C:\\ dir

            XmlTextWriter textWriter = new XmlTextWriter("C:\\myXmFile.xml", null);

            // Opens the document 

            textWriter.WriteStartDocument();


            // Write first element

            textWriter.WriteStartElement("soapenv:Envelope");

            //textWriter.WriteStartElement("r", "RECORD", "urn:record");
            textWriter.WriteStartElement("ws", "Authentication", "xmlns");


            // Write next element

            textWriter.WriteStartElement("username", "");

            textWriter.WriteString("123");

            textWriter.WriteEndElement();

            // Write one more element

            textWriter.WriteStartElement("password", "");
            textWriter.WriteString("test@123");

            textWriter.WriteEndElement();


            // Ends the document.

            textWriter.WriteEndDocument();

            // close writer

            textWriter.Close();
    }

Open in new window

myXML.xml
0
Comment
Question by:Rajkumar Gs
  • 8
  • 2
10 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 33611771
Give this a whirl:
            string soapNS = "http://schemas.xmlsoap.org/soap/envelope";
            string wsNS = "http://schemas.abc.com/message";

            XmlTextWriter writer = new XmlTextWriter("C:\\test.xml", Encoding.UTF8);
            writer.WriteStartDocument();

            // write root element and namespaces
            writer.WriteStartElement("soapenv", "Envelope", soapNS);
            writer.WriteAttributeString("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
            writer.WriteAttributeString("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
            writer.WriteAttributeString("xmlns:ws", wsNS);

            // Header
            writer.WriteStartElement("Header", soapNS);
            writer.WriteStartElement("ws:Authentication");
            writer.WriteElementString("username", "123");
            writer.WriteElementString("password", "test@123");
            writer.WriteEndElement();
            writer.WriteEndElement();
            // End of Header

            // Body
            writer.WriteStartElement("Body", soapNS);

            // createUser
            writer.WriteStartElement("ws:createUser");
            writer.WriteElementString("transID", "5105105101");
            writer.WriteElementString("propertyID", "684159968");
            writer.WriteElementString("entityID", "5101");

            // EntityDetail
            writer.WriteStartElement("EntityDetail");
            writer.WriteElementString("Description", "Test Company");
            
            // MainAddress
            writer.WriteStartElement("MainAddress");
            writer.WriteElementString("Address1", "100 Street");
            writer.WriteElementString("City", "My City");
            writer.WriteElementString("Zip", "10011");
            writer.WriteElementString("State", "My State");
            writer.WriteElementString("Country", "My Country");
            writer.WriteEndElement();
            // End of MainAddress

            // BillingAddress
            writer.WriteStartElement("BillingAddress");
            writer.WriteElementString("Address1", "100 Street");
            writer.WriteElementString("City", "My City");
            writer.WriteElementString("Zip", "10011");
            writer.WriteElementString("State", "My State");
            writer.WriteElementString("Country", "My Country");
            writer.WriteEndElement();
            // End of BillingAddress

            // End EntityDetail
            writer.WriteEndElement();

            // End createUser
            writer.WriteEndElement();

            // End Body
            writer.WriteEndElement();

            // End Envelope
            writer.WriteEndElement();

            writer.Close();

Open in new window

0
 
LVL 23

Author Comment

by:Rajkumar Gs
ID: 33611842
Thanks carl_tawn !!

After checking your code, I will post my comment

Raj
0
 
LVL 23

Author Comment

by:Rajkumar Gs
ID: 33612056
Great help Carl!

Only small problem remains.

<soapenv:Envelope
xmlns:ws="http://schemas.abc.com/message"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope">  


should be

<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ws="http://schemas.workspeed.com/message/">

Means the order in which it appeared is reversed. I tried to reverse the order of that part of the your C# code. But it is not coming in the original order. How can I correct it.

Thanks Again
Raj
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 52

Expert Comment

by:Carl Tawn
ID: 33612405
The order they come out in shouldn't matter. Is there a particular reason you need them in a specific order?
0
 
LVL 23

Author Closing Comment

by:Rajkumar Gs
ID: 33615055
No! If it will not affect the functionality, it doesn't matter.

Thanks great for your help, dude.

EE:- "Save each of the employees 3 hours a week" -- It's don't agree with it. It saves more than 3 hours :)
0
 
LVL 23

Author Comment

by:Rajkumar Gs
ID: 33615061
Can you please help me on this question as well.
http://www.experts-exchange.com/Web_Development/Web_Services/Q_26449671.html

Raj
0
 
LVL 23

Author Comment

by:Rajkumar Gs
ID: 33617437
Thanks once again Carl. I appreciate your help greatly as it saved a lot of my time.

Can you say why when I posted this XML to my webservice, showing only "Internal Server Error", instead of displaying the exact error message ?

Here is the question, which is in progress now...
http://www.experts-exchange.com/Web_Development/Web_Services/Q_26449671.html

Raj
0
 
LVL 23

Author Comment

by:Rajkumar Gs
ID: 33714687
0
 
LVL 23

Author Comment

by:Rajkumar Gs
ID: 33751807
0
 
LVL 23

Author Comment

by:Rajkumar Gs
ID: 33753728
New One - Very similar question - a Simple one!
http://www.experts-exchange.com/Programming/Languages/.NET/Q_26497236.html

Please try
Raj
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Keyboard 2 34
Xsl-fo show PDF when no results are returned 2 18
PrintingFoundIt(Almost!(Damn!)) 1 27
ASP.NET(C#) Eliminating weekends from a date range 2 37
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

832 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