无法解析C#中的XML文件#
我是一名C++开发人员,我开始研究C#WPF项目。我有一个应该读取xml文件的方法。在我的C++应用程序中,我可以非常有效地完成它,但在WPF中,我不知道如何处理这个问题。让我告诉你我的代码:无法解析C#中的XML文件#
// 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
}
}
}
我怎样才能得到标记名在完成这两个mainElement和childelem的我C++代码? :)
使用LINQ查询从XML提取数据(的XDocument)
请参阅本link对XLINQ进一步的见解。
示例XML:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<People>
<Person id="1">
<Name>Joe</Name>
<Age>35</Age>
<Job>Manager</Job>
</Person>
<Person id="2">
<Name>Jason</Name>
<Age>18</Age>
<Job>Software Engineer</Job>
</Person>
</People>
样品Linq查询:
var names = (from person in Xdocument.Load("People.xml").Descendants("Person")
where int.Parse(person.Element("Age").Value) < 30
select person.Element("Name").Value).ToList();
名称将是字符串列表。这个查询将返回贾森,因为他是18岁。
不知道这里您的XML文件的布局是,如果你知道如何使用,你可以用一个例子的LINQ
class Program
{
static void Main(string[] args)
{
XElement main = XElement.Load(@"users.xml");
var results = main.Descendants("User")
.Descendants("Name")
.Where(e => e.Value == "John Doe")
.Select(e => e.Parent)
.Descendants("test")
.Select(e => new { date = e.Descendants("Date").FirstOrDefault().Value, points = e.Descendants("points").FirstOrDefault().Value });
foreach (var result in results)
Console.WriteLine("{0}, {1}", result.date, result.points);
Console.ReadLine();
}
}
你也可以使用XPath以及解析XML文件..但倒很需要看到的XML文件格式
,如果你想要做的XMLReader使用使用System.Collections.Generic
它;使用System.Linq的 ; using System.Text;使用System.Xml的 ;
namespace XmlReading
{
class Program
{
static void Main(string[] args)
{
//Create an instance of the XmlTextReader and call Read method to read the file
XmlTextReader textReader = new XmlTextReader("C:\\myxml.xml");
textReader.Read();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(textReader);
XmlNodeList BCode = xmlDoc.GetElementsByTagName("Brandcode");
XmlNodeList BName = xmlDoc.GetElementsByTagName("Brandname");
for (int i = 0; i < BCode.Count; i++)
{
if (BCode[i].InnerText == "001")
Console.WriteLine(BName[i].InnerText);
}
Console.ReadLine();
}
}
}
在这种情况下使用XMLReader会不会更容易?为什么使用LINQ会使问题复杂化,除非它的XML文档非常复杂。 –
@DJKRAZE:首先非常感谢您的帮助:)这就是问题伴侣。 XML文件在我的办公室桌面,我不知道结构。现在我无法访问它。我可以在明天同时提供你的档案。我希望你会在Stackoverflow上线 – StonedJesus
使用Linq2Xml,
var xDoc = XDocument.Load("myfile.xml");
var result = xDoc.Descendants("i2c_write")
.Select(x => new
{
Addr = x.Attribute("addr").Value,
Count = x.Attribute("count").Value,
Radix = x.Attribute("radix").Value,
Value = x.Value,
Sleep = ((XElement)x.NextNode).Attribute("ms").Value
})
.ToList();
var khz = xDoc.Root.Element("i2c_bitrate").Attribute("khz").Value;
结帐这个StackOverflow的发布应该借给你一些想法http://stackoverflow.com/questions/8194155/c-sharp-parse-xml-file – MethodMan
你能向我们展示xml以及你和它的内容! – Anirudha
为什么你不使用'XMLReader'? –