[英]C# Read and Store Data from XML
I am trying to read and store data from an xml file. 我正在尝试从xml文件读取和存储数据。 I have been reading about various methods to read the data such as XmlReader, XmlTextReader, LinQ, etc. My XML file is 我一直在阅读各种读取数据的方法,例如XmlReader,XmlTextReader,LinQ等。我的XML文件是
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<circuit name="local">
<Device>device1</Device>
<Point>point1></Point>
</circuit>
<circuit name ="remote">
<Device>device2</Device>
<Point>point2</Point>
</circuit>
</configuration>
I am trying to extract Device and Point set so I can pass those along to be used in a database query. 我正在尝试提取设备和点集,以便可以将它们传递给数据库查询中使用。 I used this code and the foreach loop to verify the contents, but it only gets the first set. 我使用了这段代码和foreach循环来验证内容,但是它仅获取第一组。
XDocument msrDoc = XDocument.Load("BNOC MSR.config");
var data = from item in msrDoc.Descendants("circuit")
select new
{
device = item.Element("Device").Value,
point = item.Element("Point").Value
};
foreach (var p in data)
Console.WriteLine(p.ToString());
I have also tried this, but my arrays were all null 我也试过了,但是我的数组都为空
String[] deviceList = new String[1];
String[] pointList = new String[1];
int n = 0;
XmlDocument msrDoc = new XmlDocument();
msrDoc.Load("BNOC MSR.config");
var itemNodes = msrDoc.SelectNodes("circuit");
foreach (XmlNode node in itemNodes)
{
var circuit = node.SelectNodes("circuit");
foreach (XmlNode cir in circuit)
{
deviceList[n] = cir.SelectSingleNode("Device").InnerText;
pointList[n] = cir.SelectSingleNode("Point").InnerText;
}
}
Any help would be greatly appreciated. 任何帮助将不胜感激。
Are you sure you don't want to use the built-in Properties.Settings for this? 您确定不想使用内置的Properties.Settings吗?
Circuit local = Properties.Settings.Default.localCircuit;
Circuit remote = Properties.Settings.Default.remoteCircuit;
https://docs.microsoft.com/en-us/dotnet/framework/winforms/advanced/using-application-settings-and-user-settings https://docs.microsoft.com/en-us/dotnet/framework/winforms/advanced/using-application-settings-and-user-settings
I believe there is something wrong with the way you are testing the result. 我相信您测试结果的方式有问题。 The code: 编码:
void Main()
{
var fileLocation = @"C:\BrianTemp\input.txt";
var xml = File.ReadAllText(fileLocation);
XDocument msrDoc = XDocument.Load(fileLocation);
var data = from item in msrDoc.Descendants("circuit")
select new
{
device = item.Element("Device").Value,
point = item.Element("Point").Value
};
foreach (var p in data)
{
//It is best practice to use statement blocks {} to prevent silly errors.
//Sometimes you want to execute multiple statements, especially as code changes later
Console.WriteLine($"{p}");
}
}
Produces the expected output: 产生预期的输出:
{ device = device1, point = point1 }
{ device = device2, point = point2 }
You said: 你说:
I used this code and the foreach loop to verify the contents, but it only gets the first set. 我使用了这段代码和foreach循环来验证内容,但是它仅获取第一组。
As you can see the code produces 2 results as it should. 如您所见,该代码将产生2个结果。
Note: I corrected the XML file to remove the extra >
注意:我更正了XML文件,删除了多余的>
<Point>point1></Point>
<== <Point>point1></Point>
<==
I see two problems in your code (and I only tried the second method you posted): 我在您的代码中看到了两个问题(我只尝试了您发布的第二种方法):
Your string arrays are too small, change to: 您的字符串数组太小,请更改为:
String[] deviceList = new String[1]; String [] deviceList = new String [1]; String[] pointList = new String[1]; String [] pointList = new String [1];
The line var itemNodes = msrDoc.SelectNodes("circuit");
行var itemNodes = msrDoc.SelectNodes("circuit");
should be 应该
var itemNodes = msrDoc.SelectNodes("configuration"); var itemNodes = msrDoc.SelectNodes(“ configuration”);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.