【问题标题】:Xml from sql table来自sql表的xml
【发布时间】:2012-04-01 14:25:22
【问题描述】:

我想用xml获取一个sql表的内容:

projectID - projectName - customerID -customerName - city

我想在 xml 标记中列出所有列并将客户单独嵌套在项目元素中

然后我怎样才能在 .net 中正确使用它?

【问题讨论】:

    标签: asp.net .net sql-server xml


    【解决方案1】:

    您可以使用xmlelement将表格行转换为XML:

    (select xmlelement (name Project, 
       xmlattributes(p.projectID as id), 
       xmlelement(p.name as Name),
       xmlelement(name Customer,
          xmlattributes(p.customerID as id), 
          xmlforest(p.customerName as Name, p.city as City)
       ))
    )
    from 
       TableName p
    

    您需要将“TableName”替换为您的表格名称。这基本上以 XML 形式返回数据,并在父项目标签内嵌套了一个新的客户元素。这是输出的 XML:

    <Project id="1">
       <Project Name>Manhatten Project</Project>
       <Customer id="200">
          <Name>Jim Doe</Name>
          <City>New York</City>
       </Customer>
    </Project>
    

    然后您可以使用Read() 方法在.net 中解析XML。如果您之前没有在 .net 中使用过 XML,请阅读 this article 了解一般介绍。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-18
      • 2020-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-16
      相关资源
      最近更新 更多