John S
John S

Reputation: 8331

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>

Upvotes: 6

Views: 13609

Answers (4)

Zolfaghari
Zolfaghari

Reputation: 1323

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);

Upvotes: 2

Mohaisen
Mohaisen

Reputation: 1

This sample code will fix the problem.

XmlDocument layoutXml = new XmlDocument();
layoutXml.Load(MyXmlPath); //file location    

StringReader sr = new StringReader(layoutXml.DocumentElement.OuterXml);

ds.ReadXml(sr);

Upvotes: 0

Rune FS
Rune FS

Reputation: 21742

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);

Upvotes: 21

Vinay B R
Vinay B R

Reputation: 8421

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;

Upvotes: 1

Related Questions