DataSet.GetXml does not return null results

whenever I convert a DatSet to XML with a DataSet.GetXml, any null value is ignored, so where I expect this:

<value1>a</value1> <value2></value2> <value3>c</value3> 

I get this instead:

 <value1>a</value1> <value3>c</value3> 

What would be a quick and dirty way to handle this? Thanks

EDIT: I think the solution will use WriteXml. Can someone provide me with a sample of using it WITHOUT writing to a file, but get a string like GetXml does? Thanks

+4
source share
2 answers

The problem is given here in a Microsoft KB article:

http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q317961

The problem is that you do not have a schema attached to your dataset that indicates that this item should be written out.

I do not believe that using WriteXml will solve the problem, as stated in the documentation, "Calling this method is identical to calling WriteXml with XmlWriteMode set to IgnoreSchema." but you can try - here is the equivalent code:

 StringWriter sw = new StringWriter(); ds.WriteXml(sw); string outputXml = sw.ToString(); 
+4
source

This works great:

  //convert to xml with the DataSet schema: StringWriter writer = new StringWriter(); ds.WriteXml(writer, XmlWriteMode.WriteSchema); string xml = writer.ToString(); //Convert from xml to DataSet: StringReader stringReader = new StringReader(response); DataSet ds = new DataSet(); ds.ReadXml(stringReader); 
+4
source

Source: https://habr.com/ru/post/1285889/


All Articles