2013-06-03 196 views
-1

我在一個XML屬性的某個節點的特定值如何閱讀時,我的XML如下所示:獲取XML屬性值

<Settings> 
    <Display_Settings> 
    <Screen> 
     <Name Name="gadg" /> 
     <ScreenTag Tag="asfa" /> 
     <LocalPosition X="12" Y="12" Z="12" /> 
    </Screen> 
    </Display_Settings> 
</Settings> 

我只知道如何在XML的內部文本值讀取和而不是屬性值。例如,我想在LocalPosition中使用X的值。這是我迄今爲止所嘗試的;

XmlNodeList nodeList = xmlDoc.GetElementsByTagName("Screen"); 

    foreach (XmlNode nodeInfo in nodeList) 
    { 
     XmlNodeList nodeContent = nodeInfo.ChildNodes; 

     foreach (XmlNode nodeItems in nodeContent) 
     { 
      if (nodeItems.Name == "Tag") 
      { 
       print("There is a name"); 
      } 
      if (nodeItems.Name == "LocalPosition") 
      { 
       print("TEST"); 
      } 
     } 
    } 

雖然對於我想要做的事情,我認爲這是錯誤的做法。請有人指出正確的方向嗎?

回答

0

嘗試用nodeItems.Attributes["X"].Value

+0

你能解釋一下爲什麼您建議'等於()'?它的可讀性差得多,並且會像'=='一樣工作。 – svick

+0

當然, ==比較對象引用,並詢問兩個引用是否相同。 equals()比較對象內容,並詢問對象是否表示相同的概念。 – mpacheco

+1

你錯了,這不是Java。在C#中['string'重載'=='運算符](http://msdn.microsoft.com/en-us/library/system.string.op_equality.aspx)。這意味着'=='也會比較值,而不是引用。 – svick

4

您可以使用LINQ to XML

var xdoc = XDocument.Load(path_to_xml); 
int x = (int)xdoc.Descendants("LocalPosition").First().Attribute("X"); 

或用XPath

int x = (int)xdoc.XPathSelectElement("//LocalPosition").Attribute("X"); 
0
string XValue= nodeItems.Attributes["X"].Value; // will solve your problem. 
0

我有點困惑,當我第一次開始解析與C#XML太! .NET並沒有試圖自己編寫它,而是提供了System.XML.Linq和System.XML.Serialization來幫助我們解決所有困難的問題!

這種做法略有不同,因爲我們:

  1. 加載XML文檔成System.XML.Linq.XDocument,
  2. 反序列化的XDocument到.NET對象,因爲我們請的是我們可以使用= ]

我希望你不介意,但我稍微調整了你的XML例子。儘量避免在元素中使用太多屬性,因爲它們會降低可讀性。 XML示例的第一行僅突出顯示了正在使用的版本。

您應該可以將以下代碼示例複製並粘貼到新的控制檯應用程序中,以瞭解其工作原理。只要確保您的XML文件位於.. \ bin \ Debug文件夾中(否則您需要提供完整的文件路徑參考)。如你所見,在這個例子中,你的XML元素(Display_Settings,Screen,Name,ScreenTag和LocalPosition)都是我們用XMLElement屬性裝飾的類。這些類只有公共屬性,當我們調用Deserialize()時,它們會自動填充。整齊!

這同樣適用於X,Y和Z屬性。

有一堆教程可以解釋這個比我好很多 - 享受! =]

XML實例

<?xml version="1.0"?> 
<Settings> 
    <Display_Settings> 
    <Screen> 
     <Name>NameGoesHere</Name> 
     <ScreenTag>ScreenTagGoesHere</ScreenTag> 
     <LocalPosition X="12" Y="12" Z="12" /> 
    </Screen> 
    </Display_Settings> 
</Settings> 

完整的代碼示例

using System; 
using System.Xml.Linq; 
using System.Xml.Serialization; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      // Create XDocument to represent our XML file 
      XDocument xdoc = XDocument.Load("XmlFile.xml"); 

      // Create a deserializer and break down our XML into c# objects 
      XmlSerializer deserializer = new XmlSerializer(typeof(Settings)); 

      // Deserialize into a Settings object 
      Settings settings = (Settings)deserializer.Deserialize(xdoc.CreateReader()); 

      // Check that we have some display settings 
      if (null != settings.DisplaySettings) 
      { 
       Screen screen = settings.DisplaySettings.Screen; 
       LocalPosition localPosition = screen.LocalPosition; 

       // Check to make sure we have a screen tag before using it 
       if (null != screen.ScreenTag) 
       { 
        Console.WriteLine("There is a name: " + screen.ScreenTag); 
       } 

       // We can just write our integers to the console, as we will get a document error when we 
       // try to parse the document if they are not provided! 
       Console.WriteLine("Position: " + localPosition.X + ", " + localPosition.Y + ", " + localPosition.Z); 
      }    

      Console.ReadLine(); 
     } 
    } 

    [XmlRoot("Settings")] 
    public class Settings 
    { 
     [XmlElement("Display_Settings")] 
     public DisplaySettings DisplaySettings { get; set; } 
    } 

    public class DisplaySettings 
    { 
     [XmlElement("Screen")] 
     public Screen Screen { get; set; } 
    } 

    public class Screen 
    { 
     [XmlElement("Name")] 
     public string Name { get; set; } 

     [XmlElement("ScreenTag")] 
     public string ScreenTag { get; set; } 

     [XmlElement("LocalPosition")] 
     public LocalPosition LocalPosition { get; set; } 
    } 

    public class LocalPosition 
    { 
     [XmlAttribute("X")] 
     public int X { get; set; } 

     [XmlAttribute("Y")] 
     public int Y { get; set; } 

     [XmlAttribute("Z")] 
     public int Z { get; set; } 
    } 
}