wcf service returning data in xml

Posted on 2014-07-16
Last Modified: 2014-07-22
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
 <provider_Name>Dr. Abc</provider_Name>

existing is giving like as below
    <Provider_Name>Dr. Abc</Provider_Name>
    <Provider_Name>Dr. Abc</Provider_Name>
    <Provider_Name>Dr. Abc</Provider_Name>
Question by:nicemanish

    Author Comment

    Hi Admin,

    could i know why my question is neglected ...
    at least provide some suggestion is this possible or not
    LVL 96

    Expert Comment

    by:Bob Learned
    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.

    Author Comment


    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.

                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);
                string s1;
                s1 = "";
                s1 = ds.GetXml();
                return s1;
    LVL 96

    Accepted Solution

    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)
                AddElementFromRowValue(rootElement, row, "PatientId");
            private static void AddElementFromRowValue(XElement parentElement, DataRow row, string name)
                var element = new XElement(name);
                var value = row[name].ToString();
                element.Value = value;

    Open in new window

    LVL 25

    Assisted Solution

    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();

    Author Closing Comment


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
    For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now