BUG: Synchronization is not performed on DataSet and XmlDataDocument when you set InnerXml on a text node (318148)



The information in this article applies to:

  • Microsoft Visual Studio .NET (2002), Professional Edition
  • Microsoft Visual Studio .NET (2003), Professional Edition
  • Microsoft Windows .NET Framework Class Libraries 1.1
  • Microsoft XML Classes (included with the .NET Framework 1.0)

This article was previously published under Q318148

SYMPTOMS

Synchronization between DataSet(System.Data) and XmlDataDocument(System.Xml) is not performed when you use InnerXml on a node that has a text node only. The text appears to disappear from the output that is generated.

RESOLUTION

Instead of directly modifying the innerText property of the node, set the required node to an XmlNode object, and then modify the innerText property.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Use Notepad to create a new XML document that contains the following code:
    <MyDataSet>
        <MyTable1 colStringAttr="Zero">
            <colString>Zero</colString>
            <colInt>0</colInt>
            <MyTable2>
                <colString>Zero</colString>
                <colInt>0</colInt>
            </MyTable2>
        </MyTable1>
        <MyTable1 colStringAttr="One">
            <colString>One</colString>
            <colInt>1</colInt>
            <MyTable2>
                <colString>One</colString>
                <colInt>1</colInt>
            </MyTable2>
        </MyTable1>
    </MyDataSet>
    
    					
  2. Save the document as Data.xml in the root folder of your hard disk.
  3. In Visual Studio .NET, create a new Microsoft Visual C# .NET Console Application project. Replace the code in Class1.cs with the following code:
    using System;
    using System.Data;
    using System.Xml;
    using System.Xml.XPath;
    
    namespace Q318148
    {
    	class Class1
    	{
    		public static void Main()
    		{
    
    			XmlDataDocument xd = new XmlDataDocument();
    			xd.DataSet.ReadXml( "c:\\data.xml");
    			xd.DataSet.AcceptChanges();
    
    			Console.WriteLine( xd.DocumentElement.FirstChild.FirstChild.InnerXml );
    		        //Comment the following line.
    			xd.DocumentElement.FirstChild.FirstChild.InnerXml = "AA";
    			//Uncomment the following 2 lines to get the correct behavior.
    			//System.Xml.XmlNode node = xd.DocumentElement.FirstChild.FirstChild;
    			//node.InnerText ="AA";
    
    			Console.WriteLine( xd.DocumentElement.FirstChild.FirstChild.InnerXml );
    			Console.WriteLine("");
    			Console.WriteLine( xd.DataSet.GetXml());
    			
    
    		}
    	}
    }
     
    
    
    					
The following output is generated. The original value of colString is shown on the first line, followed by the replaced value. Notice that the value of colString under the MyTable1 element does not contain any values in the XML that is generated below it.
Zero
AA

<MyDataSet>
  <MyTable1 colStringAttr="Zero">
    <colString />
    <colInt>0</colInt>
    <MyTable2>
      <colString>Zero</colString>
      <colInt>0</colInt>
    </MyTable2>
  </MyTable1>
  <MyTable1 colStringAttr="One">
    <colString>One</colString>
    <colInt>1</colInt>
    <MyTable2>
      <colString>One</colString>
      <colInt>1</colInt>
    </MyTable2>
  </MyTable1>
</MyDataSet>

				
To resolve the problem:
  1. Comment the following line of code:
    xd.DocumentElement.FirstChild.FirstChild.InnerXml = "AA";
    					
  2. Uncomment the following lines of code:
    System.Xml.XmlNode node = xd.DocumentElement.FirstChild.FirstChild;
    node.InnerText ="AA";
    					
    The following output is generated. The modified node text is also reflected in the DataSet.
    Zero
    AA
    
    <MyDataSet>
      <MyTable1 colStringAttr="Zero">
        <colString>AA</colString>
        <colInt>0</colInt>
        <MyTable2>
          <colString>Zero</colString>
          <colInt>0</colInt>
        </MyTable2>
      </MyTable1>
      <MyTable1 colStringAttr="One">
        <colString>One</colString>
        <colInt>1</colInt>
        <MyTable2>
          <colString>One</colString>
          <colInt>1</colInt>
        </MyTable2>
      </MyTable1>
    </MyDataSet>
    					

Modification Type:MinorLast Reviewed:9/15/2005
Keywords:kbvs2002sp1sweep kbbug kbpending KB318148