Solved

string to xml asp.net

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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…

911 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

18 Experts available now in Live!

Get 1:1 Help Now