• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 871
  • Last Modified:

wcf service returning data in xml

i have created the wcf service which is returning me XML.attached
is the format of table and presented data.

i want the data in xml as below.please let me know how can i approched this.
i have attached the wcf service files
 
i want this structure
<Table>
 <Patientid>1</Patientid>
 <provider_Name>Dr. Abc</provider_Name>
 <Procedurename>Bajaj</procedurename>
 <Procedurename>Aviva</procedurename>
 <Procedurename>ICICI</procedurename>
</Table>


existing is giving like as below
<NewDataSet>
  <Table>
    <PatientId>1</PatientId>
    <Provider_Name>Dr. Abc</Provider_Name>
    <ProcedureName>Aviva</ProcedureName>
  </Table>
  <Table>
    <PatientId>1</PatientId>
    <Provider_Name>Dr. Abc</Provider_Name>
    <ProcedureName>Bajaj</ProcedureName>
  </Table>
  <Table>
    <PatientId>1</PatientId>
    <Provider_Name>Dr. Abc</Provider_Name>
    <ProcedureName>ICICI</ProcedureName>
  </Table>
</NewDataSet>
eeDoc.docx
Service1.svc.cs
IService1.cs
0
nicemanish
Asked:
nicemanish
  • 3
  • 2
2 Solutions
 
nicemanishAuthor Commented:
Hi Admin,

could i know why my question is neglected ...
at least provide some suggestion is this possible or not
0
 
Bob LearnedCommented:
Are you using DataSet.WriteXml?  If so, then I don't think that you can get the XML structure that you want.  I would suggest using LINQ-to-XML, and XDocument.
0
 
nicemanishAuthor Commented:
Hi,

If you see below code which i write in svc file ,it is returning xml as string .so could you please provide whats the code i have to write to achieve that xml data.


/////code
con.Open();
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter("select pt.PatientId,pr.Provider_Name,py.ProcedureName from Patient_Details pt,Provider_Details pr,Procedure_Details py where pt.Provider_Id=pr.ProviderId and pt.Procedure_Id=py.Procedure_Id and pt.PatientId= " + val1 + " Group By pt.PatientId,pr.Provider_Name,py.ProcedureName ", con);
            da.Fill(ds);
            string s1;
            s1 = "";
            s1 = ds.GetXml();
            return s1;
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Bob LearnedCommented:
Something like this might work:

using System.Data;
using System.Linq;
using System.Xml.Linq;

namespace Demo
{
    public static class DataTableWriter
    {

        /// <summary>
        /// Generate an XML document from a DataTable
        /// </summary>
        public static void WriteDataTable(DataTable dataTable, string fileName)
        {
            var document = new XDocument();

            var rootElement = new XElement("Table");

            var rowList = dataTable.Rows.OfType<DataRow>();

            var row = rowList.FirstOrDefault();

            if (row == null)
                return;

            AddElementFromRowValue(rootElement, row, "PatientId");

            document.Save(fileName);
        }

        private static void AddElementFromRowValue(XElement parentElement, DataRow row, string name)
        {
            var element = new XElement(name);

            var value = row[name].ToString();

            element.Value = value;

            parentElement.Add(element);
        }
    }
}

Open in new window

0
 
apeterCommented:
Use an extension method like below

public static string ToXml(this DataTable table, int metaIndex = 0)
{
    XDocument xdoc = new XDocument(
        new XElement(table.TableName,
            from column in table.Columns.Cast<DataColumn>()
            where column != table.Columns[metaIndex]
            select new XElement(column.ColumnName,
                from row in table.AsEnumerable()
                select new XElement(row.Field<string>(metaIndex), row[column])
                )
            )
        );

    return xdoc.ToString();
}
0
 
nicemanishAuthor Commented:
good
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now