none
XML DataTable to Clean XML

    Question

  • Hi,

    Vistual Studio 2008 – 3.5 SP1
    MySQL Server 5.1.30

    My problem: I need to use the .WriteXml method of a DataTable to write the XML to a file or stream. This I can do. The real problem is the fact that additional attributes are included in my output (IsDataSet=True, etc...), when I use the System.Data.XmlWriteMode.WriteSchema parameter on the .WriteXML method. This poses a major problem as my Web Service has to deliver neutral/agnostic XML and NOT .NET related information.

    What follows is an example of what I am returned when using the .WriteXML method with the problematic attributes bolded for emphasis.
    I need to ensure interoperability with other platforms as well as include XML schema information so that it is self-describing; what I don’t need is any Microsoft, .NET, or DataSet related attributes.

    I just can't believe that .NET does not provide a method by which to emit CLEAN (BasicProfile 1.1 compliant) XML from a DataSet and/or DataTable and inserts its own .NET specific type information.  Is what I am asking too complicated?  I thought Web Services were supposed to ensure interoperability.

    Can you please help me output neutral XML from a DataTable?


    Method: clientDataTable.WriteXml(@"C:\test.xml", System.Data.XmlWriteMode.WriteSchema);

    Output:

    <?xml version="1.0" standalone="yes"?>
    <NewDataSet>
    <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="Client" msdata:UseCurrentLocale="true">
    <xs:complexType>
    Wednesday, September 30, 2009 2:55 PM

Answers

  • Personally, I either use the XML generated by WriteXML or I built my own. I have not use XMLSerialization.

    I binged XML Serialization and found this:

    http://msdn.microsoft.com/en-us/library/ms950721.aspx

    The way I often do it is here:

    http://msmvps.com/blogs/deborahk/archive/2009/07/02/xml-literals-creating-an-xml-file.aspx

    Hope that helps.
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Marked as answer by Yichun_Feng Friday, October 2, 2009 1:43 AM
    Wednesday, September 30, 2009 3:30 PM
  • I am not sure what the WriteXml method has to do with web services. It is supposed to write out the DataSet in a way that ReadXml can read it in again.
    As for the output not being "clean", as all those attributes you complain about are in a namespace of its own the output is clean in my view.

    Have you considered to filter the output WriteXml writes, by using your own XmlWriter or an XSLT stylesheet to remove those attributes?
    MVP XML My blog
    • Marked as answer by Yichun_Feng Friday, October 2, 2009 1:44 AM
    Wednesday, September 30, 2009 4:39 PM
  • Those attributes are all in a namespace of its own and the W3C XML schema language allows custom attributes in namespace different from the W3C schema namespace so I don't understand why these attributes pose a problem.

    MVP XML My blog
    • Marked as answer by Yichun_Feng Friday, October 2, 2009 1:43 AM
    Wednesday, September 30, 2009 5:25 PM
  • Filtering out the attribute from the Microsoft specific namespace is not that hard. If you want high performant solution you can write a wrapper around XmlWriter and simply ignore calls which add these. If you want really short solution, use Linq to XML, removal of attributes from specified namespace is like 2 lines of code in it.
    As Martin pointed out, adding elements/attribute in custome namespace is usually not considered "bad", if only because it's so easy to get rid of them.

    Thanks,


    Vitek Karas [MSFT]
    • Marked as answer by Yichun_Feng Friday, October 2, 2009 1:43 AM
    Wednesday, September 30, 2009 9:37 PM
    Moderator

All replies

  • Dear Friends,

         I am currently using VS2008 (3.5 SP1) to build a .NET Web Service.  I am using a typed DataSet which contains a DataTable and a DataTableAdapter.  I am able to retrieve the information I need into a typed DataTable (ClientDataTable).  So far, so good.  The problem is that I need to export the data in the typed DataTable to XML.  While I am able to do this, I am returned XML which contains .NET DataSet specifics (IsDataSet = true, etc...).  I need to obtain ONLY the basic XML which is WS-I Basic Profile 1.1 compliant as my Web Service contains the EmitConformanceClaims and ConformsTo attributes.  In essence, the XML needs to be as agnostic and interoperable as possible as it will be used by differnet platforms.  I also need it to be self-describing so I need to include XML schema information so web service consumers know what a "Client" is and what an "Order" is; basically self-descriptive.  I am open to using LINQ, XMLWriters, XMLReaders, etc...

         In addition, I need to ensure that I am able to emit parent-child type XML (Orders -> Order).

         Can anyone please help?


         Best Regards,

              Giovanni
    • Merged by Yichun_Feng Thursday, October 1, 2009 8:27 AM duplicate
    Friday, September 25, 2009 2:09 PM
  • Not sure if this helps, but you always try.

                DataTable dt = new DataTable();
                dt.WriteXml("x", XmlWriteMode.IgnoreSchema);
    This way only data is writen, however i'm not sure if it is WS-I Basic Profille 1.1 complient

    Kind regards,

    Eric van Glabbeek
    Willing to learn everything about Sharepoint there is to learn. My blog : sharepoint.vanglabbeek.nl
    Wednesday, September 30, 2009 12:13 PM
  • Hi Eric,

         Thank you for your response.  I like the suggestion unfortunately, I need the XMLSchema appended to the result as it must be self-describing so that developers can bind to it and know what a "Customer" or a "Client" is and their associated structure.  I just can't believe that .NET does NOT offer a clean way to generate XML from a DataSet or DataTable.  I have reworded my question and will repost it.  Maybe the newly worded question will help.  Please let me know if you discovered anything else.


         Giovanni
    Wednesday, September 30, 2009 2:50 PM
  • You can certainly build your own XML by looping through the rows of the DataTable.

    Or you can use OO and build business objects instead of a datatable and use XMLSerialization.

    Hope this helps.
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Wednesday, September 30, 2009 3:12 PM
  • Hi DeborahK,

         Thank you for your prompt response.  I was really trying to avoid having to implement the looping scenario as I also need to create and append the schema as well and I am sure this will complicate things.  Would you have any additional information on your proposed solution or code examples to get me going?  I can't believe the hoops I have to go through to emit basic XML.

        
         Giovanni
    Wednesday, September 30, 2009 3:16 PM
  • Personally, I either use the XML generated by WriteXML or I built my own. I have not use XMLSerialization.

    I binged XML Serialization and found this:

    http://msdn.microsoft.com/en-us/library/ms950721.aspx

    The way I often do it is here:

    http://msmvps.com/blogs/deborahk/archive/2009/07/02/xml-literals-creating-an-xml-file.aspx

    Hope that helps.
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Marked as answer by Yichun_Feng Friday, October 2, 2009 1:43 AM
    Wednesday, September 30, 2009 3:30 PM
  • Hi,

         Thanks again for your prompt reply.  I will try to use some of the methods in the links you have sent.  I cannot use the XML Literals per say as I am using C#.  I will try the XML Serialization route and see if that helps.  Thanks again.  Maybe someone will be able to reply with a more automatic/less code intensive method.  Incredible.


         Giovanni
    Wednesday, September 30, 2009 3:35 PM
  • I am not sure what the WriteXml method has to do with web services. It is supposed to write out the DataSet in a way that ReadXml can read it in again.
    As for the output not being "clean", as all those attributes you complain about are in a namespace of its own the output is clean in my view.

    Have you considered to filter the output WriteXml writes, by using your own XmlWriter or an XSLT stylesheet to remove those attributes?
    MVP XML My blog
    • Marked as answer by Yichun_Feng Friday, October 2, 2009 1:44 AM
    Wednesday, September 30, 2009 4:39 PM
  • Hi Martin,

         I am trying to use the WriteXml method to return information from a DataTable or DataSet.  It does, as you mentioned, write out the DataSet in a way that ReadXml can read it again but this is .NET specific and cannot be consumed by JAVA, and other problems in a simple way without having to parse the result somehow.  It is not XML agnostic, rather it contains .NET DataSet specific attributes such as IsDataSet=true.  This is great if ALL my web service consumers were to be .NET based but they are not.  They have no way of knowing what a "Client" object is supposed to look like as the information returned by WriteXml (though the schema is included), cannot be interpreted by other platforms easily if at all.  I guess I can filter out the output by using my own XmlWriter or an XSLT, but I was hoping to avoid this extra step.  As well, I do not have much experience with XSLT stylesheets.


         Giovanni
    Wednesday, September 30, 2009 4:45 PM
  • Those attributes are all in a namespace of its own and the W3C XML schema language allows custom attributes in namespace different from the W3C schema namespace so I don't understand why these attributes pose a problem.

    MVP XML My blog
    • Marked as answer by Yichun_Feng Friday, October 2, 2009 1:43 AM
    Wednesday, September 30, 2009 5:25 PM
  • Filtering out the attribute from the Microsoft specific namespace is not that hard. If you want high performant solution you can write a wrapper around XmlWriter and simply ignore calls which add these. If you want really short solution, use Linq to XML, removal of attributes from specified namespace is like 2 lines of code in it.
    As Martin pointed out, adding elements/attribute in custome namespace is usually not considered "bad", if only because it's so easy to get rid of them.

    Thanks,


    Vitek Karas [MSFT]
    • Marked as answer by Yichun_Feng Friday, October 2, 2009 1:43 AM
    Wednesday, September 30, 2009 9:37 PM
    Moderator
  • Vitek,

    Can you give an code example for

    Filtering out the attribute from the Microsoft specific namespace is not that hard. If you want high performant solution you can write a wrapper around XmlWriter and simply ignore calls which add these. If you want really short solution, use Linq to XML, removal of attributes from specified namespace is like 2 lines of code in it.

    Many dev's are not up to speed on Linq, and thus they do not grasp the simplicity.

    Ken Lassesen, ex-MSDN Dr.GUI
    Tuesday, January 26, 2010 8:42 PM
  • Hi,

    Sorry about the short answer. Here's a sample, it removes all attributes in the specified namespace from the entire document:
                XDocument doc = XDocument.Load("myxml.xml");
    
                XNamespace namespaceToRemove = XNamespace.Get("uri");
                doc.Descendants().Attributes().Where(a => a.Name.Namespace == namespaceToRemove).Remove();
                Console.WriteLine(doc);
    


    Thanks,
    Vitek
    Vitek Karas [MSFT]
    Wednesday, January 27, 2010 12:49 AM
    Moderator
  • Many thanks, I have actually coded up a Dataset extension which will likely make life easier for all,
    Ken Lassesen, ex-MSDN Dr.GUI
    Wednesday, January 27, 2010 8:30 PM
  • Hi ,

    If you want to remove the names space from the xml ,then look the logic of this forum

    http://forums.asp.net/p/1483243/3467101.aspx
        class NamespaceStripper
        {
            const string xmlnsNs = "http://www.w3.org/2000/xmlns/";
            const string defaultNs = "xmlns";
            XmlDocument output;
    
            public XmlDocument StripNamespace(XmlDocument input)
            {
                output = new XmlDocument();
                output.PreserveWhitespace = true;
                foreach (XmlNode child in input.ChildNodes)
                {
                    output.AppendChild(StripNamespace(child));
                }
                return output;
            }
    
            XmlNode StripNamespace(XmlNode inputNode)
            {
                XmlNode outputNode = output.CreateNode(inputNode.NodeType, inputNode.LocalName, null);
    
                // copy attributes, stripping namespaces
                if (inputNode.Attributes != null)
                {
                    foreach (XmlAttribute inputAttribute in inputNode.Attributes)
                    {
                        if (!(inputAttribute.NamespaceURI == xmlnsNs || inputAttribute.LocalName == defaultNs))
                        {
                            XmlAttribute outputAttribute = output.CreateAttribute(inputAttribute.LocalName);
                            outputAttribute.Value = inputAttribute.Value;
                            outputNode.Attributes.Append(outputAttribute);
                        }
                    }
                }
    
                // copy child nodes, stripping namespaces
                foreach (XmlNode childNode in inputNode.ChildNodes)
                {
                    outputNode.AppendChild(StripNamespace(childNode));
                }
    
                // copy value for nodes without children
                if (inputNode.Value != null)
                {
                    outputNode.Value = inputNode.Value;
                }
    
                return outputNode;
            }
        }



    Please mark this post as Answer if it is of help to you!

    " Every wall is a door..! "

    Regards,
    ANKIT
    ANKIT BHATNAGAR
    Thursday, January 28, 2010 10:42 AM