我正在尝试读取XML文件并解析其内容,但我无法从文件中提取参数。
我正在尝试解析的XML文件如下所示:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<register_map>
<Register ID="1" Index="0x100000" DataType="0x0007" ObjectType="0x07" Name="Device Type"/>
<Register ID="2" Index="0x100100" DataType="0x0005" ObjectType="0x07" Name="Error Register"/>
</register_map>
</root>
到目前为止我的代码看起来像这样
namespace Test_XML
{
class Program
{
struct RegisterEntry
{
public UInt32 index;
public UInt16 dataType;
public UInt16 objectType;
public string name;
};
static void Main(string[] args)
{
XDocument doc = XDocument.Load("registers.xml");
var registers = doc.Descendants("register_map");
foreach (var register in registers)
{
// Fill up a list of RegisterEntrys with contents of XML
}
Console.ReadLine();
}
}
}
如何从“寄存器”中提取参数并将它们放在RegisterEntry
对象中?
答案 0 :(得分:3)
您可以使用
var registers = doc.XPathSelectElements("/root/register_map/Register");
它将为您提供Register
个节点的集合,因此您将能够访问其属性并填充RegisterEntry
对象,如:
foreach (var register in registers)
{
var dataType = register.Attribute("DataType").Value;
//the rest of the code
}
注意XPathSelectElements
是System.Xml.XPath
命名空间中的扩展方法。确保您已引用System.Xml.Linq
程序集以便使用它。
答案 1 :(得分:1)
您应该使用.Attributes["name"].Value
。我认为你想要将这些值转换为Int,所以我们还需要一个额外的Convert.ToInt(string, base);
var RegisteryEntryList = new List<RegistryEntry>();
foreach (var register in registers)
{
//create a new RegistryEntry
var obj = new RegistryEntry();
//convert your string to an int value and save it
obj.index = Convert.ToInt32(register.Attributes["Index"].Value.Split('x')[1], 8);
obj.datatype = Convert.ToInt32(register.Attributes["DataType"].Value.Split('x')[1], 8);
//... your remaining properties
RegisteryEntryList.Add(obj);
}
请注意:如果您的索引是二进制(基数2),则需要相应地调整转换。有关详细信息,请参阅https://msdn.microsoft.com/en-us/library/1k20k614(v=vs.110).aspx
答案 2 :(得分:1)
您的查询将为您提供名称为register_map
的所有元素 - 您想要所有Register
个元素。将其更改为:
var registers = doc.Descendants("Registers");
然后遍历它们并获取所需的值,将它们转换为所需的类型。
foreach (var register in registers)
{
var indexHex = (string)register.Attribute("Index");
var index = Convert.ToUInt32(indexHex, 16);
var dataTypeHex = (string)register.Attribute("DataType");
var dataType = Convert.ToUInt16(dataTypeHex, 16);
var objectTypeHex = (string)register.Attribute("ObjectType");
var objectType = Convert.ToUInt16(objectTypeHex, 16);
var name = (string)register.Attribute("Name");
var entry = new RegisterEntry
{
index = index,
dataType = dataType,
objectType = objectType,
name = name,
};
// do something with entry
}
答案 3 :(得分:1)
使用xml Linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Globalization;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string xml =
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
"<root>" +
"<register_map>" +
"<Register ID=\"1\" Index=\"0x100000\" DataType=\"0x0007\" ObjectType=\"0x07\" Name=\"Device Type\"/>" +
"<Register ID=\"2\" Index=\"0x100100\" DataType=\"0x0005\" ObjectType=\"0x07\" Name=\"Error Register\"/>" +
"</register_map>" +
"</root>";
XDocument doc = XDocument.Parse(xml);
var results = doc.Descendants("Register").Select(x => new {
id = (int)x.Attribute("ID"),
index = int.Parse(x.Attribute("Index").Value.Substring(2), NumberStyles.HexNumber, CultureInfo.CurrentCulture),
dataType = int.Parse(x.Attribute("DataType").Value.Substring(2), NumberStyles.HexNumber, CultureInfo.CurrentCulture),
objectType = int.Parse(x.Attribute("ObjectType").Value.Substring(2), NumberStyles.HexNumber, CultureInfo.CurrentCulture),
name = (string)x.Attribute("Name")
}).ToList();
}
}
}