0

How to create attribute based xml in c# from stored procedure returning result

Stored Procedure contains:

Select * from Patients

I want XML:

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

I am getting this:

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

2 Answers 2

2

The easiest would be to change your proc to return an xml string generated by FOR XML AUTO

Select * 
FROM  Patients AS Patient
FOR XML AUTO;

SqlFiddle here

Sign up to request clarification or add additional context in comments.

Comments

1

Assuming you are getting result as DataSet from Stored Procedure.

You can setColumnMapping, and then write to xml. .

DataTable dt ; // Patients information retrieved from db.

foreach (DataColumn column in dt.Columns)
{
    column.ColumnMapping = MappingType.Attribute;
}

dt.WriteXml(@"C:\Patients.xml");  

Hope this helps !!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.