5

目前我将 xml 存储在 ms sql dbase 而不是文件中。这是一个小样本。

<NewDataSet>
<Table1>
<billTo_lastName>asdf</billTo_lastName>
<orderAmount>160.00</orderAmount>
<billTo_street1>asdf</billTo_street1>
<card_accountNumber>############1111</card_accountNumber>
</Table1>
</NewDataSet>

目前我在Dataable中返回结果。

解析上述内容并将其显示在页面上的最佳方法是什么。页面显示仅供参考。不会对 xml 进行额外的处理。

我希望页面显示这些内容。

billTo_lastName: asdf
orderAmount: 160.00
etc.
4

3 回答 3

7

Depends on how you want to process it. One way is to create an XmlDocument and then call LoadXml:

// get the data from the data table, into a string.

// then create an XML document and load the string
var doc = new XmlDocument();
doc.LoadXml(dataString);

If you want to use Linq-to-Xml, you'd create an XElement:

var element = XElement.Load(dataString);
于 2013-03-11T22:08:00.167 回答
4

感谢吉姆为我指明了正确的方向。XmlDocuments LoadXml 工作得很好。然后我能够将其放入 XmlNodeList 并选择顶部节点。做了 XmlNode 并循环遍历子节点。

    DataTable dt = sqlselect(sqlQuery, parameter);
    var doc = new XmlDocument();
    doc.LoadXml(dt.Rows[0]["ua_post"].ToString());

    XmlNodeList nl = doc.SelectNodes("NewDataSet");
    XmlNode root = nl[0];

    foreach (XmlNode xnode in root.ChildNodes[0])
    {
        string name = xnode.Name;
        string value = xnode.InnerText;
        string nv = "<b>" + name + ":</b> " + value;
        Label1.Text += nv + " <br />" + Environment.NewLine;
    }

我在页面上得到了一个简单的显示。

    billTo_lastName: asdf 
    orderAmount: 160.00 
    billTo_street1: asdf 
    card_accountNumber: ############1111 
于 2013-03-12T14:02:53.707 回答
0

您可以将它们作为列从数据库返回到 DataTable,然后使用任何带有 ItemTemplates 的数据控件显示它们例如:

declare @xml as xml
set @xml = '<NewDataSet>
<Table1>
<billTo_lastName>asdf</billTo_lastName>
<orderAmount>160.00</orderAmount>
<billTo_street1>asdf</billTo_street1>
<card_accountNumber>############1111</card_accountNumber>
</Table1>
</NewDataSet>'

;with cte as (    
  select @xml xmlstring
)
SELECT 
xmlstring.value('(/NewDataSet//Table1/billTo_lastName/node())[1]','VARCHAR(100)')
 as billTo_lastName,
xmlstring.value('(/NewDataSet//Table1/orderAmount/node())[1]','VARCHAR(20)') 
as orderAmount
FROM cte

SQL 演示

于 2013-03-11T22:14:44.947 回答