1

我的查询选择 xml 中的字段。我需要的是不在一行中显示xml

<AllPlayers><Player><fbid>1236598</fbid><fbname>joan</fbname><fbscore>99999999</fbscore></Player><Player><fbid>55559999888</fbid><fbname>smith</fbname><fbscore>99999999</fbscore></Player></AllPlayers>

但作为:

<AllPlayers>
  <Player>
    <fbid>1236598</fbid>
    <fbname>Mohamed Hamam</fbname>
    <fbscore>99999999</fbscore>
  </Player>
  <Player>
    <fbid>55559999888</fbid>
    <fbname>mostafaa Hamam</fbname>
    <fbscore>99999999</fbscore>
  </Player>
</AllPlayers>

我的查询:

ALTER PROCEDURE [dbo].[procGetPlayerScore]
AS BEGIN
    SELECT DISTINCT TOP (10)
        fbid ,   
        Name as fbname,
        Score  as fbscore        
    FROM 
        dbo.FB_Player
    ORDER BY 
        Score DESC 
    FOR XML  PATH('Player'), ROOT('AllPlayers')
END

asp页面:

Response.Expires = -1;
Response.ContentType = "text/xml";

using(System.Data.SqlClient.SqlConnection c = new SqlConnection(ConfigurationManager.ConnectionStrings["x"].ConnectionString))
using (System.Data.SqlClient.SqlCommand cmd = c.CreateCommand())
{
    cmd.CommandText = "procGetPlayerScore";
    cmd.CommandType = CommandType.StoredProcedure;

    c.Open();
    System.Xml.XmlReader r = cmd.ExecuteXmlReader();
    string playersXml = cmd.ExecuteScalar().ToString();
    xmlc.InnerText = playersXml;
    System.Xml.XmlTextWriter w = new System.Xml.XmlTextWriter(Response.Output);

    c.Close();
}

在此处输入图像描述

4

1 回答 1

0

You need to specify Formatting in XmlTextWriter like bellow: (I assume that xmlc is XmlDocument)

[your code]
System.Xml.XmlTextWriter w = new System.Xml.XmlTextWriter(Response.Output);
w.Formatting = Formatting.Indented;
xmlc.WriteContentTo(w);
c.Close();
}

Update

Below updated code based on comments:

[your code]
string playersXml = cmd.ExecuteScalar().ToString();
System.Xml.XmlTextWriter w = new System.Xml.XmlTextWriter(Response.Output);
w.Formatting = Formatting.Indented;
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

using (var stringWriter = new StringWriter())
 using (var xmlTextWriter = XmlWriter.Create(stringWriter))
 {
   xmlDoc.WriteTo(xmlTextWriter);
   xmlTextWriter.Flush();
   playersXml  = stringWriter.GetStringBuilder().ToString();
 }

xmlc.InnerText = playersXml;
c.Close();
}
于 2013-09-04T14:21:59.653 回答