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

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
MedtronicVascSRAsked:
Who is Participating?
 
Carlos VillegasFull Stack .NET DeveloperCommented:
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
 
Carlos VillegasFull Stack .NET DeveloperCommented:
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
 
MedtronicVascSRAuthor Commented:
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
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
Carlos VillegasFull Stack .NET DeveloperCommented:
Good to know, have a nice day.
0
 
MedtronicVascSRAuthor Commented:
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
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Hello, I dont understand very well what you meant, you want to be able to render the Date property only in some circumstances?
0
 
MedtronicVascSRAuthor Commented:
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
 
Carlos VillegasFull Stack .NET DeveloperCommented:
OK! I got it
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
I get it :p
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.