Dataset.ReadXML 在路径中返回无效字符。为什么?

发布于 2024-09-19 23:13:00 字数 576 浏览 5 评论 0原文

我正在使用 ReadXML 方法将字符串读入 DataSet 中。当我尝试这样做时,它返回路径错误中的无效字符。如果我在 IE 中将字符串保存为 xml 文件并打开,它会在 encoding="UTF-16" 行上引发错误,因此我认为这就是问题的原因。

有没有简单的方法来解决这个问题?它不应该能够处理 unicode 或 UTF-16 吗?

任何建议将不胜感激。使用 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

自在安然 2024-09-26 23:13:10

我认为您可以尝试使用 ReadStartElement 前进到下一个节点并将整个表读入 DataSet。

XmlTextReader r = new XmlTextReader(@"c:\b.xml");
r.MoveToContent();
r.ReadStartElement("iCall");
DataSet ds = new DataSet();
ds.ReadXml(r);
this.dataGrid1.DataSource = ds;

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;
遇见了你 2024-09-26 23:13:10

此示例代码将解决该问题。

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

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

ds.ReadXml(sr);

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);
活雷疯 2024-09-26 23:13:09

最好使用额外的行 XmlTextReader xtr = ... 并将 xtr 传递给 ReadXml 方法。

DataSet ds = new DataSet();
StringReader sr = new StringReader(strXml); // or xdoc.InnerXml
XmlTextReader xtr = new XmlTextReader(sr);
ds.ReadXml(xtr);

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);
纵情客 2024-09-26 23:13:07

DataSet.ReadXml(string) 需要文件路径而不< /em> 一个 xml 文档。因此,它尝试将您的 xml 文档解析为文件路径,

如果您只有 XML 运行时,则会失败,那么您可以这样做:

StringReader sr = new StringReader(xml);
dataSet.ReadXml(sr);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文