使用 C# 读取 XML-File 中节点的 ID
Reading the ID of an Node in XML-File using C#
我在使用 C# 读取 XML 文件中的变量时遇到问题。
我有以下 XML-文件:
<?xml version="1.0" encoding="utf-8"?>
<list xmlns:xsd=...>
<row id="2b7bed1a-bd72-4e83-9b69-26c05cce6379">
<hostname>TestName1</hostname>
<outputs>
<outputId>HL7 Output</outputId>
</outputs>
</row>
<row ...
</row>
</list>
我想在这里写一个代码,它读取 XML-File 并使用循环获取每个主机名的 id。
我已经编写了以下部分,它加载了 xml-文件。有人可以帮我完成这个吗?
var xmlFile = @"C:\Users\vosoughi-m\Desktop\output_mapping.xml";
var doc = new XmlDocument();
doc.Load(xmlFile);
提前致谢
编辑:
我想要以下输出:
主机名:TestName1
编号:2b7bed1a-hd72-4e83-9b69-66c05cce6379
主机名:TestName2
编号:ce4372d3-453e-4ed8-ba60-6694a2680c24
...
使用xml linq :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication21
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
var results = doc.Descendants("row")
.Select(x => new { id = (string)x.Attribute("id"), hostname = (string)x.Element("hostname") }).ToList();
}
}
}
我在使用 C# 读取 XML 文件中的变量时遇到问题。
我有以下 XML-文件:
<?xml version="1.0" encoding="utf-8"?>
<list xmlns:xsd=...>
<row id="2b7bed1a-bd72-4e83-9b69-26c05cce6379">
<hostname>TestName1</hostname>
<outputs>
<outputId>HL7 Output</outputId>
</outputs>
</row>
<row ...
</row>
</list>
我想在这里写一个代码,它读取 XML-File 并使用循环获取每个主机名的 id。
我已经编写了以下部分,它加载了 xml-文件。有人可以帮我完成这个吗?
var xmlFile = @"C:\Users\vosoughi-m\Desktop\output_mapping.xml";
var doc = new XmlDocument();
doc.Load(xmlFile);
提前致谢
编辑:
我想要以下输出:
主机名:TestName1
编号:2b7bed1a-hd72-4e83-9b69-66c05cce6379
主机名:TestName2
编号:ce4372d3-453e-4ed8-ba60-6694a2680c24
...
使用xml linq :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication21
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
var results = doc.Descendants("row")
.Select(x => new { id = (string)x.Attribute("id"), hostname = (string)x.Element("hostname") }).ToList();
}
}
}