Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dataset.ReadXML returns invalid characters in path. Why?

I'm reading a string into a DataSet using the ReadXML method. When I try that it returns an Invalid Characters in the path error. if I save and open the string in IE as an xml file it throws an error on the encoding="UTF-16" line so I assume that is the cause of the problem.

Is there a simple way to fix this? Shouldn't it be able to handle unicode or UTF-16?

Any suggestions would be much appreciated. Using C# & .Net 4

<?xml version="1.0" encoding="UTF-8" ?> 
 <iCall xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <Rows>
 <Row>
  <Code /> 
  <Title>Test Title</Title> 
  </Row>
  </Rows>
</iCall>
like image 554
John S Avatar asked Sep 13 '10 17:09

John S


3 Answers

DataSet.ReadXml(string) expects a file path not an xml document. So it tries to parse your xml document as a filepath and fails

if you only have your XML runtime, then you can do like this:

StringReader sr = new StringReader(xml);
dataSet.ReadXml(sr);
like image 63
Rune FS Avatar answered Oct 20 '22 19:10

Rune FS


It is better to use an extra line XmlTextReader xtr = ... and pass xtr to ReadXml method.

DataSet ds = new DataSet();
StringReader sr = new StringReader(strXml); // or xdoc.InnerXml
XmlTextReader xtr = new XmlTextReader(sr);
ds.ReadXml(xtr);
like image 45
Zolfaghari Avatar answered Oct 20 '22 18:10

Zolfaghari


I think you can try to use ReadStartElement to advance to the next node and read the whole table into DataSet.

XmlTextReader r = new XmlTextReader(@"c:\b.xml");
r.MoveToContent();
r.ReadStartElement("iCall");
DataSet ds = new DataSet();
ds.ReadXml(r);
this.dataGrid1.DataSource = ds;
like image 1
Vinay B R Avatar answered Oct 20 '22 19:10

Vinay B R