2012-10-10 101 views
0

我是一名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 
     } 
    } 
} 

我怎樣才能得到標記名在完成這兩個mainElementchildelem我C++代碼? :)

+0

結帳這個StackOverflow的發佈應該借給你一些想法http://stackoverflow.com/questions/8194155/c-sharp-parse-xml-file – MethodMan

+0

你能向我們展示xml以及你和它的內容! – Anirudha

+0

爲什麼你不使用'XMLReader'? –

回答

0

使用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歲。

+1

OP是C++開發者Rakesh,所以也許你可以展示一個代碼示例來幫助他..希望他理解Linq – MethodMan

+0

@StonedJesus:不用擔心隊友......上面的例子對你有幫助嗎?您還可以使用LINQPad直觀地創建查詢。 http://www.linqpad.net/ – RockWorld

+0

爲什麼我的答案是downvoted ?? – RockWorld

1

不知道這裏您的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(); 
     } 
    } 
} 
+0

在這種情況下使用XMLReader會不會更容易?爲什麼使用LINQ會使問題複雜化,除非它的XML文檔非常複雜。 –

+0

@DJKRAZE:首先非常感謝您的幫助:)這就是問題伴侶。 XML文件在我的辦公室桌面,我不知道結構。現在我無法訪問它。我可以在明天同時提供你的檔案。我希望你會在Stackoverflow上線 – StonedJesus

0

使用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;