?
Solved

How To Suppress Certain Class Items On XML Output For Web Service

Posted on 2011-10-14
9
Medium Priority
?
360 Views
Last Modified: 2012-05-12
Hi,
I have web service that outputs to xml more information than I'd like to output. I need to understand how to suppress some of the class fields that appear in the xml output that I don't reference explicitly. It seems item types such as "DateTime" and "bool" automatically get added, whereas "string" does not.

1. The first snippet makes the call and returns the list in xml format
2. The second snippet has the classes with the data elements
3. The third snippet is how I create the list
4. The image is the result.

As you can see in the image despite only assigning "Product_Name" to the "obj" from myReader (in bold)
                SqlDataReader myReader = null;

                string sqlStatement = "SELECT " +
                    " Product_Name " +
                    " FROM Physician_Training_Lookup " +
                    " WHERE Physician_Training_Lookup.Physician_Info_ID = " + @searchCriteria +
                    " ORDER BY Physician_Training_Lookup.Product_Name ";

                SqlCommand SqlCommand = new SqlCommand(sqlStatement, SqlServer);
                SqlCommand.Parameters.Add(new SqlParameter("@searchCriteria", searchCriteria));

                myReader = SqlCommand.ExecuteReader();
                while (myReader.Read())
                {
                    obj = new PhysicianTraining();

                    //Bring in the product value from the Product class
                    obj.Product = new Product();
                    Product TrainingProd = new Product();
                    obj.Product.Name = myReader["Product_Name"].ToString();                              

                    physicianList.Add(obj);
                }

 the output brings back many of the other class items, such as Date, Product_ID, Fellow, etc.

What's the best way to suppress the class items that seem to want to come through?

Cheers,
Ty
[Description("Get a hydrated physician list in XML format")]
        [WebGet(UriTemplate = "/physiciantraining.xml?searchCriteria={searchCriteria}", ResponseFormat = WebMessageFormat.Xml)]
        [OperationContract]
        [XmlSerializerFormat]
        public List<PhysicianTraining> GetPhysicianTrainingList(string searchCriteria)
        {
            clsPhysicianTrainingList = new EndoPhysicianData();
            List<PhysicianTraining> resultList = clsPhysicianTrainingList.GetPhysicianTrainingList(searchCriteria: searchCriteria);
            return resultList;
        }

Open in new window

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Medtronic.Mobile.EndoPhysician.BusinessObjects
{
    public class PhysicianTraining
    {
        public DateTime Date { get; set; }
        public Product Product { get; set; }
        public Physician Physician { get; set; }
        public string SignatureImageBytes { get; set; } 
        public Account AffiliatedAccount { get; set; }
        public User CCE { get; set; }
        public bool Fellow { get; set; }
        public string FellowGradYear { get; set; }
        public bool VS { get; set; }
        public bool IR { get; set; }
        public bool CT { get; set; }
        public bool IC { get; set; }

    }
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Medtronic.Mobile.EndoPhysician.BusinessObjects
{
    public class Product
    {
        public int Product_ID { get; set; }
        public string Name { get; set; }
        public string Checklist { get; set; }
    }
}

Open in new window

public List<PhysicianTraining> GetPhysicianTrainingList(string searchCriteria)
        {

            //Make sure something was passed
            if (String.IsNullOrEmpty(searchCriteria))
            {
                searchCriteria = "0";
            }

            var physicianList = new List<PhysicianTraining>();
            var obj = new PhysicianTraining();

            //Open the connection
            try
            {
                SqlServer.Open();
            }
            catch (Exception e)
            {
                ErrorMessageOpening = e.ToString();
            }


            //Run the sql query
            try
            {
                SqlDataReader myReader = null;

                string sqlStatement = "SELECT " +
                    " Product_Name " +
                    " FROM Physician_Training_Lookup " +
                    " WHERE Physician_Training_Lookup.Physician_Info_ID = " + @searchCriteria +
                    " ORDER BY Physician_Training_Lookup.Product_Name ";

                SqlCommand SqlCommand = new SqlCommand(sqlStatement, SqlServer);
                SqlCommand.Parameters.Add(new SqlParameter("@searchCriteria", searchCriteria));

                myReader = SqlCommand.ExecuteReader();
                while (myReader.Read())
                {
                    obj = new PhysicianTraining();

                    //Bring in the product value from the Product class
                    obj.Product = new Product();
                    Product TrainingProd = new Product();
                    obj.Product.Name = myReader["Product_Name"].ToString();

                    physicianList.Add(obj);
                }
            }
            catch (Exception e)
            {
                ErrorMessageSQLSelect = e.ToString();
            }



            //Close the connection
            try
            {
                SqlServer.Close();
            }
            catch (Exception e)
            {
                ErrorMessageClosing = e.ToString();
            }

            return physicianList;
        }

Open in new window

ArrayXML.jpg
0
Comment
Question by:MedtronicVascSR
  • 6
  • 3
9 Comments
 
LVL 17

Accepted Solution

by:
Carlos Villegas earned 1000 total points
ID: 36970208
Hello, I think that you can use the System.Xml.Serialization.XmlIgnoreAttribute attribute, example:
    public class PhysicianTraining
    {
        public DateTime Date { get; set; }

        [XmlIgnore]// to avoid show the Product on your xml
        public Product Product { get; set; }

        public Physician Physician { get; set; }
        public string SignatureImageBytes { get; set; } 
        public Account AffiliatedAccount { get; set; }
        public User CCE { get; set; }
        public bool Fellow { get; set; }
        public string FellowGradYear { get; set; }
        public bool VS { get; set; }
        public bool IR { get; set; }
        public bool CT { get; set; }
        public bool IC { get; set; }
    }

Open in new window

0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 36970231
If the last example does not work, then try with the System.NonSerializedAttribute attribute, example:
    public class PhysicianTraining
    {
        public DateTime Date { get; set; }

        [NonSerialized()]// to avoid show the Product on your xml
        public Product Product { get; set; }

        public Physician Physician { get; set; }
        public string SignatureImageBytes { get; set; } 
        public Account AffiliatedAccount { get; set; }
        public User CCE { get; set; }
        public bool Fellow { get; set; }
        public string FellowGradYear { get; set; }
        public bool VS { get; set; }
        public bool IR { get; set; }
        public bool CT { get; set; }
        public bool IC { get; set; }
    }

Open in new window

But I think that the first example is good for your case.
0
 

Author Closing Comment

by:MedtronicVascSR
ID: 36970378
Thank you. Works like a charm.

using System.Xml.Serialization;
[XmlIgnoreAttribute]

I read up on this to understand why:
"By default, all public fields and public read/write properties are serialized by the XmlSerializer. That is, the value of each public field or property is persisted as an XML element or XML attribute in an XML-document instance."

Cheers,
Ty
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 36970395
Good to know, have a nice day.
0
 

Author Comment

by:MedtronicVascSR
ID: 36970560
Hi,
I shoud've tested the scenario where I would eventually need to output one of the items that I have now ignored. I mistakenly assumed that when I referenced the item it would display, but apparently you need to override [XmlIgnoreAttribute] if you want to display it. I'm reading through some of the documentation at the moment on how to do it. Off the top of your head do you know to that might look:

myReader = SqlCommand.ExecuteReader();
                while (myReader.Read())
                {
                    obj = new PhysicianTraining();

                    //Bring in the product value from the Product class
                    obj.Product = new Product();
                    Product TrainingProd = new Product();
                    obj.Product.Name = myReader["Product_Name"].ToString();

                    //need to somehow override the [XmlIgnoreAttribute] attribute this item
                    obj.Date = Convert.ToDateTime(myReader["Date_Trained"]);

                    physicianList.Add(obj);
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 36970612
Hello, I dont understand very well what you meant, you want to be able to render the Date property only in some circumstances?
0
 

Author Comment

by:MedtronicVascSR
ID: 36970654
I was just using the Date property as an example since all the supporting code was already posted. It's not necessarily the property or even the class I'd want to work with, but understanding how to override it would work elsewhere. Since some of classes often have properties that may or may not want to be outputed based on the needs of the specific web service by default most should be ignored. I see that you can use "XmlAttributeOverrides" on the fly when you want to override the ignore attribute for a specific property. I just need to tweak how to integrate it into my current code.

Sorry for the confusion.
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 36970740
OK! I got it
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 36970744
I get it :p
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here I am going to explain creating proxies at runtime for WCF Service. So basically we use to generate proxies using Add Service Reference and then giving the Url of the WCF service then generate proxy files at client side. Ok, what if something ge…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

580 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