【发布时间】:2012-10-10 16:53:03
【问题描述】:
我是一名 C++ 开发人员,我开始从事 C# WPF 项目。我有一个应该读取 xml 文件的方法。在我的 c++ 应用程序中,我可以非常有效地做到这一点,但在 WPF 中,我不确定如何解决这个问题。让我给你看我的代码:
// When Browse Button is clicked this method is called
private void ExecuteScriptFileDialog()
{
var dialog = new OpenFileDialog { InitialDirectory = _defaultPath };
dialog.DefaultExt = ".xml";
dialog.Filter = "XML Files (*.xml)|*.xml";
dialog.ShowDialog();
ScriptPath = dialog.FileName; //ScriptPath contains the Path of the Xml File
if (File.Exists(ScriptPath))
{
LoadAardvarkScript(ScriptPath);
}
}
public void LoadAardvarkScript(string ScriptPath)
{
// I should read the xml file
}
我在 C++ 中的实现如下:
File file = m_selectScript->getCurrentFile(); //m_selectScript is combobox name
if(file.exists())
{
LoadAardvarkScript(file);
}
void LoadAardvarkScript(File file)
{
XmlDocument xmlDoc(file);
//Get the main xml element
XmlElement *mainElement = xmlDoc.getDocumentElement();
XmlElement *childElement = NULL;
XmlElement *e = NULL;
int index = 0;
if(!mainElement )
{
//Not a valid XML file.
return ;
}
//Reading configurations...
if(mainElement->hasTagName("aardvark"))
{
forEachXmlChildElement (*mainElement, childElement)
{
//Read Board Name
if (childElement->hasTagName ("i2c_write"))
{
// Some code
}
}
}
如何像在我的 c++ 代码中那样获得 mainElement 和 childelem 的 tagname? :)
【问题讨论】:
-
查看这个 StackOverFlow 发布它应该会给你一些想法stackoverflow.com/questions/8194155/c-sharp-parse-xml-file
-
你能给我们看看 xml 以及你和它的内容吗!
-
你为什么不直接使用
XMLReader? -
@Ramhound:很遗憾我是这个 c# wpf 世界的新手。我还没做呢
-
我刚刚将它发布到我的第一个答案的底部。如果您可以显示您的 XML 文件的格式,它也会有所帮助,因为如果您知道我的意思,有很多方法可以给猫剥皮