Solved

string to xml asp.net

Posted on 2014-11-05
5
329 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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 …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
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…

860 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