How to Get the Xml Data from a DataSet when the co

2019-09-06 14:55发布

问题:

I have a stored procedure which returns 2 xml resultsets as below:

SELECT 1 as SiteID
FOR XML PATH('FirstResultSet'), TYPE

SELECT 1 as UserID
FOR XML PATH('SecondResultSet'), TYPE

I have run my stored procedure and got the DataSet object.

I called the GetXml method and got the xml:

string xml = dataSet.GetXml();

But the xml is as below which is not in the expected format:

<NewDataSet>
<Table><Column1>&lt;FirstResultSet&gt;&lt;SiteID&gt;1&lt;SiteID&gt;&lt;FirstResultSet&gt;</Column1></Table>
<Table1><Column1>&lt;SecondResultSet&gt;&lt;UserID&gt;1&lt;UserID&gt;&lt;SecondResultSet&gt;</Column1></Table1>
</NewDataSet>

As you see the retrieved text is treated as text rather than xml.

How to get the xml out of this? I know I can just replace "<" and ">" but is there any built-in method for this to make this easier and quicker with better performance?

string xml = dataSet.GetXml().Replace("&lt;", "<").Replace("&gt;", ">");

Thanks,

回答1:

Try:

string xml = Server.HtmlDecode(dataSet.GetXml());

http://msdn.microsoft.com/en-us/library/7c5fyk1k.aspx

Maybe you need the exact opposite:

string xml = Server.HtmlEncode(dataSet.GetXml());

http://msdn.microsoft.com/en-us/library/w3te6wfz.aspx