1

How can I get readable xml string from a SQL Server database?

In this way I get xml string, then set it to ExtJs textarea. But text is not readable:

public string GetXml(string args)
{
    string exMsg = "";
    string result = "";
    StringBuilder xmlResult = new StringBuilder();

    Dictionary<string, object> parameters = _ser.Deserialize<Dictionary<string, object>>(args);
    string Id = parameters["Id"].ToString();

    try
    {
        System.Data.SqlClient.SqlConnection cn = new System.Data.SqlClient.SqlConnection();

        cn.ConnectionString = "data source=(local);initial catalog=MethodSettings;User ID=sa;Password=sasa";

        cn.Open();

        System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
        cmd.Connection = cn;
        cmd.CommandText = string.Format("SELECT MethodArgs FROM  MethodSettings.dbo.MethodParameters  WHERE Id ={0}", Id);

        System.Xml.XmlReader xmlr = cmd.ExecuteXmlReader();

        xmlr.Read();

        if (xmlr != null)
        {
            xmlResult.AppendLine(xmlr.ReadOuterXml());
            result = xmlResult.ToString();
        }
    }
    catch (Exception ex)
    {
         exMsg = ex.Message;
    }

    return _ser.Serialize(result);
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Gani
  • 37
  • 1
  • 6
  • possible duplicate? http://stackoverflow.com/questions/1123718/format-xml-string-to-print-friendly-xml-string – Rodrigo López Apr 25 '15 at 12:42
  • what do you mean exactly by '*not readable*'? can you give example XML you expect to see and what actually get displayed? – har07 Apr 25 '15 at 13:12
  • for example i get : ToveJaniReminderDon't forget me this weekend! I want to get like here: [link](http://www.w3schools.com/xml/note.xml) – Gani Apr 25 '15 at 13:34

2 Answers2

0

Try this

            System.Xml.XmlReader xmlr = cmd.ExecuteXmlReader();
            XmlDocument doc = new XmlDocument();
            doc.Load(xmlr);​
jdweng
  • 33,250
  • 2
  • 15
  • 20
0

You can use XDocument's ToString() method to get formatted XML string easily, if that's what you actually want to get :

System.Xml.XmlReader xmlr = cmd.ExecuteXmlReader();
XDocument doc = XDocument.Load(xmlr);
doc.Load(xmlr);​

var result = doc.ToString();
har07
  • 88,338
  • 12
  • 84
  • 137