MedtronicVascSR
asked on
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_Na me ";
SqlCommand SqlCommand = new SqlCommand(sqlStatement, SqlServer);
SqlCommand.Parameters.Add( new SqlParameter("@searchCrite ria", 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"].T oString();
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
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.
" ORDER BY Physician_Training_Lookup.
SqlCommand SqlCommand = new SqlCommand(sqlStatement, SqlServer);
SqlCommand.Parameters.Add(
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"].T
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;
}
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; }
}
}
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;
}
ArrayXML.jpg
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
Good to know, have a nice day.
ASKER
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"].T oString();
//need to somehow override the [XmlIgnoreAttribute] attribute this item
obj.Date = Convert.ToDateTime(myReade r["Date_Tr ained"]);
physicianList.Add(obj);
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"].T
//need to somehow override the [XmlIgnoreAttribute] attribute this item
obj.Date = Convert.ToDateTime(myReade
physicianList.Add(obj);
Hello, I dont understand very well what you meant, you want to be able to render the Date property only in some circumstances?
ASKER
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.
Sorry for the confusion.
OK! I got it
I get it :p
Open in new window
But I think that the first example is good for your case.