?
Solved

string to xml asp.net

Posted on 2014-11-05
5
Medium Priority
?
349 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 2000 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

762 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