【问题标题】:How to create attribute based xml in c#如何在 C# 中创建基于属性的 xml
【发布时间】:2015-06-17 05:48:15
【问题描述】:

如何在c#中从存储过程返回结果创建基于属性的xml

存储过程包含:

Select * from Patients

我想要 XML:

<Patient patientid =”1”  domainid =”1” domainname =” Test Domain”/>

我得到了这个:

<Patient>
    <patientid>1</patientid>
    <domainid>1</domainid>
    <domainname>Test Domain</domainname>
  </Patient>

【问题讨论】:

    标签: c# sql xml


    【解决方案1】:

    最简单的方法是更改​​您的 proc 以返回由 FOR XML AUTO 生成的 xml 字符串

    Select * 
    FROM  Patients AS Patient
    FOR XML AUTO;
    

    SqlFiddle here

    【讨论】:

      【解决方案2】:

      假设您从存储过程中获得DataSet 的结果。

      可以设置ColumnMapping,然后写入xml。 .

      DataTable dt ; // Patients information retrieved from db.
      
      foreach (DataColumn column in dt.Columns)
      {
          column.ColumnMapping = MappingType.Attribute;
      }
      
      dt.WriteXml(@"C:\Patients.xml");  
      

      希望对你有帮助!!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-06-04
        • 1970-01-01
        • 2016-04-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多