Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 156571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:21:04+00:00 2026-05-11T10:21:04+00:00

I’m having some issues using XmlSerializer and XmlTextReader in c# when saving DataTables which

  • 0

I’m having some issues using XmlSerializer and XmlTextReader in c# when saving DataTables which do not contain any data. Is this a known issue and is there a workaround? When an empty datatable is saved using XMLSerializer the following XML is generated:

      <Values>         <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='Values' msdata:UseCurrentLocale='true'>             <xs:complexType>               <xs:choice minOccurs='0' maxOccurs='unbounded'>                 <xs:element name='Values'>                   <xs:complexType>                   </xs:complexType>                 </xs:element>               </xs:choice>             </xs:complexType>           </xs:element>         </xs:schema>         <diffgr:diffgram xmlns:msdata='urn:schemas-microsoft-com:xml-msdata' xmlns:diffgr='urn:schemas-microsoft-com:xml-diffgram-v1' />       </Values> 

When the XML containing this is reloaded XMLTextReader fails silently and does not load content beyond the point at which the empty datatable is written to the XML. This issue appears to be caused by the lack of an xs:sequence / xs:element entry inside xs:complexType. Is this a bug and if so what is the workaround?

The following c# program demonstrates the issue. It will output dt3 is null due to the issue described above:

public class Data {     private DataTable dt1;     private DataTable dt2;     private DataTable dt3;      public DataTable Dt1     {         get { return dt1; }         set { dt1 = value; }     }      public DataTable Dt2     {         get { return dt2; }         set { dt2 = value; }     }      public DataTable Dt3     {         get { return dt3; }         set { dt3 = value; }     }      public void TestDataTables()     {         if(dt1 == null)             Console.WriteLine('dt1 is null');         if (dt2 == null)             Console.WriteLine('dt2 is null');         if (dt3 == null)             Console.WriteLine('dt3 is null');     } } class Program {     static void Main(string[] args)     {         // Create test object         Data data = new Data();         data.Dt1 = new DataTable('Test1');         data.Dt1.Columns.Add('Foo');         data.Dt2 = new DataTable('Test2');         // Adding the following line make serialization work as expected         //data.Dt2.Columns.Add('Foo');         data.Dt3 = new DataTable('Test3');         data.Dt3.Columns.Add('Foo');         data.TestDataTables();          // Save to XML         TextWriter filewriter = new StreamWriter('foo.xml');         XmlTextWriter writer = new XmlTextWriter(filewriter);         writer.Formatting = Formatting.Indented;         XmlSerializer s1 = new XmlSerializer(typeof(Data));         s1.Serialize(writer, data);         writer.Close();         filewriter.Close();          // Reload from XML         TextReader filereader = new StreamReader('foo.xml');         XmlTextReader reader = new XmlTextReader(filereader);         XmlSerializer s2 = new XmlSerializer(typeof(Data));         Data newData = s2.Deserialize(reader) as Data;         newData.TestDataTables();      } } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T10:21:04+00:00Added an answer on May 11, 2026 at 10:21 am

    (after the edit)

    Thanks for the example code. Yes, I’d agree that this is a bug. For workarounds… well, you could add a column by default? To avoid the cross-contamination, you could use ShouldSerialize*:

    public bool ShouldSerializeDt1() {     return dt1 != null && dt1.Columns.Count > 0; } public bool ShouldSerializeDt2() {     return dt2 != null && dt2.Columns.Count > 0; } public bool ShouldSerializeDt3() {     return dt3 != null && dt3.Columns.Count > 0; } 

    At least then it is dt2 that gets omitted, instead of dt3.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 141k
  • Answers 141k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I don't think you can really do this with sed,… May 12, 2026 at 8:04 am
  • Editorial Team
    Editorial Team added an answer You can handle the events on the view itself, or… May 12, 2026 at 8:04 am
  • Editorial Team
    Editorial Team added an answer Since you are rolling your own session management logic, you… May 12, 2026 at 8:04 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.