none
DataSet.GetXml() - Nest Related Data RRS feed

  • Question

  • Hi,

    I have a DataSet with two tables, with a relationship defined between them. When I execute GetXml() I would therefore expect the XML that is generated to have the two sets of data nested in a parent / child type layout, rather than a simple dump of the first table, followed by the second.

    Does that make sense..?  Is it possible to achieve..?

    Also, the entire DataSet in the XML output is surrounded by CDATA tags - How can I avoid that..?

    Cheers.

    Sunday, May 22, 2011 10:23 AM

Answers

  • Is it possible to achieve..?

    No, not by using the .GetXml() method. You'd have to write a custom method to do that I think. The XML string, as it's produced by the .GetXml() method is in the proper format to then be read back in to another DataSet with the .ReadXml() method. Why do you need it in a nested format?

    Also, the entire DataSet in the XML output is surrounded by CDATA tags - How can I avoid that..?

    That's strange ... what kind of data are you storing in your DataSet? I've never seen CDATA tags produced by the output of the .GetXml() method.


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Marked as answer by TDSnet Monday, May 23, 2011 10:32 AM
    Sunday, May 22, 2011 4:36 PM

All replies

  • Is it possible to achieve..?

    No, not by using the .GetXml() method. You'd have to write a custom method to do that I think. The XML string, as it's produced by the .GetXml() method is in the proper format to then be read back in to another DataSet with the .ReadXml() method. Why do you need it in a nested format?

    Also, the entire DataSet in the XML output is surrounded by CDATA tags - How can I avoid that..?

    That's strange ... what kind of data are you storing in your DataSet? I've never seen CDATA tags produced by the output of the .GetXml() method.


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Marked as answer by TDSnet Monday, May 23, 2011 10:32 AM
    Sunday, May 22, 2011 4:36 PM
  • Thanks Bonnie,

    I was looking for that particular layout so that if the consuming application is not .NET and therefore can't just re-inflate a DataSet they at least have some meaningful XML to work with.

    And in fact even if they are using .NET, that means essentially iterating through the parent table, creating DataViews of the child table to return only the related rows.

    I've only ever returned custom business objects which are purpose built to deliver the data in the friendliest manner but the current project I am working on requires me to return DataSets and I just don't particularly like what I am returning.

    With regards to the CDATA tags, I just realised that might not be the GetXml() method's doing. I am wrapping the entire GetXml() response in our standard opening and closing tags which are <response></response> and the CDATA starts immediately AFTER the opening <response> tag.  I am putting it all together using a StringBuilder:

    StringBuilder ResponseXML = new StringBuilder();
    ResponseXML.AppendLine("<Response>");
    ResponseXML.AppendLine(ITO.GetUpdatedInbound(lastUpdated, targetWarehouseId).GetXml());
    ResponseXML.AppendLine("</Response>");
    return ResponseXML.ToString();
    
    Can you see any issue there..?

    Sunday, May 22, 2011 11:01 PM
  • And in fact even if they are using .NET, that means essentially iterating through the parent table, creating DataViews of the child table to return only the related rows.

    Are you using .xsd's to define your DataSets? If so, the consuming application can be given the .xsd (and even generate the Typed DataSet for their app) and know ahead of time what the DataSet looks like and can define the DataRelation between parent/child and thus won't have to worry about creating DataViews. Then just read the XML into the new Typed DataSet.

    Can you see any issue there..?
    ResponseXML.AppendLine(ITO.GetUpdatedInbound(lastUpdated, targetWarehouseId).GetXml());

    I assume that ITO.GetUpdatedInbound(lastUpdated, targetWarehouseId) simply returns a DataSet and I see no reason why CDATA would be stuck in there. Is this XML being returned via an ASMX Web Service or some other Service (WCF)? My understanding of CDATA is that it's used when there is data in the XML that the XML Parser should not parse ... things like HTML tags, I think (unless they get changed to &lt and &gt).  I've only run into that with ASMX Web Service methods for columns that contained "\r\n" in .NET or SQL Server and having to parse/format that to the HTML version, "&#13;" so that it would be passed correctly through the Web Service. Basically I had two methods in my Web Service class that would convert between the two depending on whether I was sending XML back through a Web Service method, or had just received XML from the Web Service method. I don't know if a similar thing is happening to your data, but it's something to look into.

     


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Sunday, May 22, 2011 11:54 PM
  • Thanks Bonnie - Greatly appreciate the responses.
    Monday, May 23, 2011 10:33 AM
  • Not that I was all that much help .... ;0)

    Let me know if you figure out where that CDATA tag came from ...


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Tuesday, May 24, 2011 2:35 PM