?
Solved

Control Serialized Grouping On XML Output For Web Service

Posted on 2011-10-18
7
Medium Priority
?
254 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:MedtronicVascSR
  • 4
  • 3
7 Comments
 
LVL 6

Accepted Solution

by:
regevha earned 1000 total points
ID: 36987990
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
 

Author Comment

by:MedtronicVascSR
ID: 36989156
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
 
LVL 6

Expert Comment

by:regevha
ID: 36990405
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:regevha
ID: 36990428
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
 

Author Closing Comment

by:MedtronicVascSR
ID: 36990592
Thanks regevha,
You have pointed me in the right direction with all the resource links.

Cheers,
Ty
0
 
LVL 6

Expert Comment

by:regevha
ID: 36990675
Thanks Ty. I am glad the issue is sorted out. Do you wish to attach a code snippet ?
0
 

Author Comment

by:MedtronicVascSR
ID: 36996070
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

864 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