C#sql查询为xml中的元素添加属性

时间:2011-05-04 06:38:14

标签: c# .net sql sql-server xml

我想通过从XML_TAGS表中获取标记以及MAPPED_TAGS_ATTRIBUTES表中的属性来显示XML文件 - 类似于<element attributes></element>

String sql = "SELECT Dtd_Tag,Dtd_Attribute_Name 
                FROM Xml_Tags,
                     Mapped_Tags_Attributes 
               WHERE Mapped_Tags_Attributes.Pdf_Tag = Xml_Tags.Pdf_Tag 
            ORDER BY Mapped_Tags_Attributes.Pdf_Tag 
             FOR XML AUTO"; 

1 个答案:

答案 0 :(得分:1)

查询:

select
    c.CustomerId as "@Id",
    c.AcountNumber as "@AcountNumber"
from Customer c
for xml path('Customer'), root('Customers')

结果:

<Customers>
   <Customer Id="1" AccountNumber="X120" />
   <Customer Id="2" AccountNumber="X121" />
</Customers>