Solved

string to xml asp.net

Posted on 2014-11-05
5
316 Views
Last Modified: 2014-11-20
Below codes, myDatas will return xml string, and I want xml string to be saved as xml document in c:\\server\\folder\

How can I do that?


 [WebMethod]
        public string HelloWorld2()
        {
            string errorMessage = "";
            string myDatas = "";
            //Connection string is stored
            //in the web.config file as an appSetting
            string connectionString = "Data Source=LAX-MJHEHHK;Initial Catalog=Surety;Integrated Security=True";
            SqlConnection dbConnection = null;
            // Open a connection to the database
            try
            {
                dbConnection = new SqlConnection(connectionString);
                dbConnection.Open();
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
            }
            if (errorMessage == "")
            {
                // Build an insert command
                string SQL = "SELECT PrincipalID,PrincipalName FROM Principals FOR XML AUTO, ELEMENTS";
                SqlCommand GetCustomerCmd = new SqlCommand(SQL, dbConnection);
                try
                {
                    XDocument results = new XDocument(new XElement("results"));
                    using (XmlReader reader = GetCustomerCmd.ExecuteXmlReader())
                    {
                        reader.Read();
                        while (!reader.EOF)
                        {
                            myDatas = myDatas + reader.ReadOuterXml();
                        }                        
                    }
                    dbConnection.Close();
                }
                catch (System.Exception ex)
                {
                    errorMessage = ex.Message;
                }
                finally
                {
                    dbConnection.Dispose();
                }
            }
            return myDatas;
        }
0
Comment
Question by:ITsolutionWizard
  • 3
  • 2
5 Comments
 
LVL 35

Expert Comment

by:Miguel Oz
ID: 40425052
Could you post a sample of your xml string?
The solution could use LoadXml method as follows:
XmlDocument doc= new XmlDocument();
doc.LoadXml("myXmlString");
doc.PreserveWhitespace = true
doc.Save("Your file full path")

Open in new window

0
 

Author Comment

by:ITsolutionWizard
ID: 40425097
my file name is C:\Program Files, and it does not work ....

doc.Save("C:\Program Files")
0
 
LVL 35

Expert Comment

by:Miguel Oz
ID: 40425109
It has to be a valid windows xml filename:
c:\\server\\folder\myxmlname.xml
You cannot store it to a folder. By the it is not a good idea to save under folder "C:\Program Files" it should only contain folders of the programs you installed in the PC.
0
 

Author Comment

by:ITsolutionWizard
ID: 40425185
not working.

it complaints "There are multiple root elements."


here is result from xml

<?xml version="1.0" encoding="UTF-8"?>
<string xmlns="http://tempuri.org/">

<Principals>
<PrincipalID>1</PrincipalID>
<PrincipalName>Sam</PrincipalName>
</Principals>

<Principals>
<PrincipalID>2</PrincipalID>
<PrincipalName>Mary</PrincipalName>
</Principals>

</string>
0
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 500 total points
ID: 40446201
Q1. Could you post the contents of "myDatas" after being read from database? (breakpoint at dbConnection.Close();)
Q2. Where in your code you are consuming the xml string? My solution assumes that you are planning to save myDatas before sending that info to your client program.

"Multiple root..." error means there is no one root node in your xml string. The typical solution is to create a unique parent node as shown in Rob's answer
For example if you are capturing the response in the client:
1) Remove the following constructs
<?xml version="1.0" encoding="UTF-8"?>
 <string xmlns="http://tempuri.org/">
</string>
2) Create a unique parent node (<PrincipalRoot>) so that your new string will look like
<PrincipalRoot>
<Principals>
 <PrincipalID>1</PrincipalID>
 <PrincipalName>Sam</PrincipalName>
 </Principals>

 <Principals>
 <PrincipalID>2</PrincipalID>
 <PrincipalName>Mary</PrincipalName>
 </Principals>
</PrincipalRoot>
3) Now save this string using my previous posted code.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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