当我的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 :(得分:4)
您可以使用LINQ to XML:
var xdoc = XDocument.Load(path_to_xml);
int x = (int)xdoc.Descendants("LocalPosition").First().Attribute("X");
int x = (int)xdoc.XPathSelectElement("//LocalPosition").Attribute("X");
答案 1 :(得分:0)
尝试使用nodeItems.Attributes["X"].Value
答案 2 :(得分:0)
string XValue= nodeItems.Attributes["X"].Value; // will solve your problem.
答案 3 :(得分:0)
当我第一次开始用C#解析XML时,我有点困惑!而不是试图自己编写.NET,提供System.XML.Linq和System.XML.Serialization来帮助我们处理所有困难的东西!
这种方法与我们略有不同:
我希望你不介意,但我稍微调整了你的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; }
}
}