Solved

string to xml asp.net

Posted on 2014-11-05
5
341 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
[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
5 Comments
 
LVL 36

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
 
LVL 1

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 36

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
 
LVL 1

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 36

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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

688 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