Control Serialized Grouping On XML Output For Web Service

Hi,
I have a question on some web service code I've inherited. Specifically, about the grouping that automatically displays in XML. I see that they are driven by both class names (i.e.. UserTraining, Product) and class property (ApprovedProducts under the UserTraining class).
 
    public class UserTraining
    {
        public string UserName { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public List<Product> ApprovedProducts { get; set; }
    }

    public class Product
    {
        [XmlIgnoreAttribute]
        public int Product_ID { get; set; }
        public string Name { get; set; }
        public string Checklist { get; set; }
    }

 
So, when I run the query and read in the results the output looks like this as expected:
 
 <?xml version="1.0" encoding="utf-8" ?>
- <ArrayOfUserTraining xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <UserTraining>
      <UserName>hildrb2</UserName>
      <FirstName>Brad</FirstName>
      <LastName>Hildreth</LastName>
-     <ApprovedProducts>
-         <Product>
              <Name>AneuRx</Name>
      </Product>
  </ApprovedProducts>
</UserTraining>
- <UserTraining>
      <UserName>hildrb2</UserName>
      <FirstName>Brad</FirstName>
      <LastName>Hildreth</LastName>
-     <ApprovedProducts>
        - <Product>
          <Name>Talent_Abdominal</Name>
      </Product>
  </ApprovedProducts>
</UserTraining>
</ArrayOfUserTraining>
 
However, the client would like the endpoint to look like this without the UserTraining and repetitive ApprovedProducts grouping.
 
<?xml version="1.0" encoding="UTF-8" ?>
- <User xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <UserName>t.cr.d</UserName>
  <FirstName>Test</FirstName>
  <LastName>User</LastName>
- <ApprovedProducts>
-     <Product>
          <Name>Converter Occluder</Name>
  </Product>
    + <Product>
          <Name>Cardiac Resync</Name>
    </Product>
     </ApprovedProducts>
</User>

Though my initial observation is that the grouping in the XML output are driven by the class names, etc I'm not sure how to control the output groupings to something other than them while using the following code block to create the userList that gets returned to be serialized.

                while (myReader.Read())
                {
                    obj = new UserTraining();

                    obj.UserName = myReader["Employee_Number"].ToString();
                    obj.FirstName = myReader["First_Name"].ToString();
                    obj.LastName = myReader["Last_Name"].ToString();

                    //Bring in the CCE Training list from the Product class
                    obj.ApprovedProducts = new List<Product>();
                    Product Training = new Product();
                    //read in the product value
                    Training.Name = myReader["Product_Name"].ToString();
                    //add it to the Trainings object from the Physician class
                    obj.ApprovedProducts.Add(Training);

                    userList.Add(obj);
                }


I could run a separate code block that outputs the User's name, but I'm not sure how to control product display. My first thought is to gather the data from the query in an array and then manually create the xml with my own groupings. Other less code intensive thoughts?

Cheers,
Ty
MedtronicVascSRAsked:
Who is Participating?
 
regevhaCommented:
You should define an XSL transformation between the XML created by the serialization and the XML the client expects (convert from one XML structure to another one).
Please have a look at solution http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SQL-Server-2005/Q_27189780.html?sfQueryTermInfo=1+10+30+c+transform+xslt
0
 
MedtronicVascSRAuthor Commented:
Thanks regevha,
If I understand the process after reviewing the documentation on "XML transformation using Xslt", I simply run the generated XML file through the Transformation function and it utilizes the format I have specified in the XSL file to produce the XML results with a different set of children and parents, correct? So, the followup question I would have is since an actual XML file is not being saved, but rather generated as part of the web service being returned to the iPad, would the XML "path" be the URI submitted by the web service?

Cheers,
Ty
0
 
regevhaCommented:
Hello Ty,

For the transformation, use XslCompiledTransform.Transform Method:
http://msdn.microsoft.com/en-us/library/system.xml.xsl.xslcompiledtransform.aspx

There are a few different signatures to the Transform method. You may use String or XmlReader as desribed in detail in the MSDN link above and it can be based on XML data stored in memory.

If you would like me to provide code sample, please explain the exact proccessing required in more detail.

Good luck,
Regev
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
regevhaCommented:
Additional link on using LoadXml for loading data into an XmlDocument (without using URI, I.e. no physical file):
 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.loadxml.aspx
0
 
MedtronicVascSRAuthor Commented:
Thanks regevha,
You have pointed me in the right direction with all the resource links.

Cheers,
Ty
0
 
regevhaCommented:
Thanks Ty. I am glad the issue is sorted out. Do you wish to attach a code snippet ?
0
 
MedtronicVascSRAuthor Commented:
I would but unfortunately before I could start coding the solution I got a call that the client wants the web servive return format changed from XML to Json. So, I've had to go another direction on this. Fortunately, simply posting this question opened up a couple cool solutions for transforming XML data that I can use in other projects.

Thanks again for you assistance and direction!
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.