0

私はプロジェクト (C#) に取り組んできましたが、その一部は、埋め込まれた xml ファイルでデータ グリッドを埋めていました。

これを機能させる方法を見つけましたが、その背後にある理論についてはまだ混乱しています。そして、このプロジェクトを続行する前に、停止して完全に理解していることを確認したいと思います.

私が現在取り組んでいるコードは次のとおりです。

XmlDataDocument myXML = new XmlDataDocument();  
StringReader mytempXML = (new StringReader(BasicTest.Properties.Resources.myxml));
myXML.DataSet.ReadXml(mytempXML);

私が混乱しているのは、この解決策の前に、以下を試していたことです。

myXML.DataSet.ReadXml(BasicTest.Properties.Resources.myxml);

そしてそれは機能していませんでした。ただし、完全なファイル パス (以下のように) を使用しても機能していました。

myXML.DataSet.ReadXml("C:/..etc../myxml.xml");

私が持っている質問は次のとおりです。リソースから読み取る場合、ReadXml メソッドに StringReader が必要なのはなぜですか?完全なファイル パスを使用すると、それがなくても機能します。

誰かが説明を提供できれば、それは素晴らしいことです。ありがとう。

4

1 に答える 1

0

This is because the ReadXml method takes a string. That string must be the name of a file. It cannot be XML. If you pass it a string that is XML, it will think that is the name of the file! It doesn't have the smarts to look at the string and ask "Is this string XML, or is it a file name?" and figure that out.

// Summary:
//     Reads XML schema and data into the System.Data.DataSet using the specified
//     file.
//
// Parameters:
//   fileName:
//     The filename (including the path) from which to read.
public XmlReadMode ReadXml(string fileName);

By wrapping the XML in a stringreader or a stream or something, you are calling a different overload, that expects XML instead of a file name.

于 2014-01-28T16:05:49.887 に答える