如何访问具有属性值的 XML 文件中的元素?
How to access element in XML files with attribute values?
这是我的 XML 文件
<colleges>
<college college_name="DYPSOE">
<departments>
<department department_name="Computer" id="10">
<![CDATA[I NEED TO CHANGE THIS COMMENT!]]>
</department>
<department department_name="Machanical" id="20">
<![CDATA[I NEED TO CHANGE THIS COMMENT!]]>
</department>
</departments>
</college>
<college college_name="DYPSOET">
<departments>
<department department_name="Computer" id="10">
<![CDATA[I NEED TO CHANGE THIS COMMENT!]]>
</department>
<department department_name="Machanical" id="20">
<![CDATA[I NEED TO CHANGE THIS COMMENT!]]>
</department>
</departments>
</college>
</colleges>
我在程序中有三个属性值 college_name、department_name 和 id。所以我想转到特定的 "Department" 节点并使用这三个属性值更改注释中的值。
我正在尝试通过不同的查询访问节点,到目前为止失败了。
var node = from e in doc.Descendants("college")
where e.Attribute("college_name").ToString() == college_name
select (XElement)e.Elements("department");
foreach (XElement data in node)
{
Console.WriteLine(data); //Just to look what I got
data.Value = ""; //To change the comment section
}
这根本不起作用。如果你们能给我建议查询,那将对我有很大帮助。
我想你可以使用 System.Xml.XmlReader
to read the nodes' attribute and write it with System.Xml.XmlWriter
How to: Parse XML with XmlReader有解析写入的例子
假设您想要 select 一个基于学院和部门名称的单个部门元素,您可以使用如下查询找到它:
var query = from college in doc.Descendants("college")
where (string) college.Attribute("college_name") == "DYPSOE"
from department in college.Descendants("department")
where (string) department.Attribute("department_name") == "Computer"
select department;
var element = query.Single();
然后您可以像这样替换评论:
element.ReplaceNodes(new XCData("new comment"));
这是我的 XML 文件
<colleges>
<college college_name="DYPSOE">
<departments>
<department department_name="Computer" id="10">
<![CDATA[I NEED TO CHANGE THIS COMMENT!]]>
</department>
<department department_name="Machanical" id="20">
<![CDATA[I NEED TO CHANGE THIS COMMENT!]]>
</department>
</departments>
</college>
<college college_name="DYPSOET">
<departments>
<department department_name="Computer" id="10">
<![CDATA[I NEED TO CHANGE THIS COMMENT!]]>
</department>
<department department_name="Machanical" id="20">
<![CDATA[I NEED TO CHANGE THIS COMMENT!]]>
</department>
</departments>
</college>
</colleges>
我在程序中有三个属性值 college_name、department_name 和 id。所以我想转到特定的 "Department" 节点并使用这三个属性值更改注释中的值。
我正在尝试通过不同的查询访问节点,到目前为止失败了。
var node = from e in doc.Descendants("college")
where e.Attribute("college_name").ToString() == college_name
select (XElement)e.Elements("department");
foreach (XElement data in node)
{
Console.WriteLine(data); //Just to look what I got
data.Value = ""; //To change the comment section
}
这根本不起作用。如果你们能给我建议查询,那将对我有很大帮助。
我想你可以使用 System.Xml.XmlReader
to read the nodes' attribute and write it with System.Xml.XmlWriter
How to: Parse XML with XmlReader有解析写入的例子
假设您想要 select 一个基于学院和部门名称的单个部门元素,您可以使用如下查询找到它:
var query = from college in doc.Descendants("college")
where (string) college.Attribute("college_name") == "DYPSOE"
from department in college.Descendants("department")
where (string) department.Attribute("department_name") == "Computer"
select department;
var element = query.Single();
然后您可以像这样替换评论:
element.ReplaceNodes(new XCData("new comment"));