c# - C#:如何按值从 Xml 文件中选择单个节点(元素?)?
问题描述
我是 Xml 的新手,试图创建一个简单的数据库,我可以从相应的 C# 控制台应用程序读取和写入该数据库。
我加载了 Xml 文件,然后生成 Xml 元素“string_ID”的 C# 列表(希望成为每个节点中的标识符 - 将显示代码 ** 注意:不是实际的 Xml ID 功能)。如果用户输入与列表中的 string_ID 匹配(我假设我可以剪切这部分,但这基本上是我问题的核心),那么程序选择相应的 Xml 元素并增加计数节点(?)。
我无法根据用户输入选择单个节点来工作。好吧,我根本无法让基于“string_ID”的单节点选择工作——就像硬编码我的测试值一样。
我之前在网上找到的一些尝试:
1)
XmlElement root = xDoc.DocumentElement;
var result = root.GetElementsByTagName("prizeUnit")
.Where(i => (string)i.Element("string_ID") == "333AI")
.Seelect(i => (string)i.Element("Folder"));
- 我最近的; .Where等甚至不注册。
2)
XmlNode node = xDoc.DocumentElement.SelectSingleNode(
'//prizeUnit/string_ID[text()="333AI"]');
- 注意单引号和双引号。
3)
XmlNode node = xDoc.DocumentElement.SelectSingleNode(
"root_prizes/prizeUnit/string_ID[" + input + "]");
4)
XmlNode node = xDoc.DocumentElement.SelectSingleNode(
"root_prizes/prizeUnit/[string_ID/text() = \"input\"]");
- 这是案例 #2 之前的案例。\" \" 对我来说似乎值得注意。
我的 XML 文件:
<root_prizes xmlns="http://tempuri.org/XMLSchema.xsd">
<prizeUnit>
<string_ID>333AI</string_ID>
<prizeName>cashGrandPrize01</prizeName>
<count>1313</count>
</prizeUnit>
<prizeUnit>
<string_ID>334BI</string_ID>
<prizeName>cashGrandPrize02</prizeName>
<count>2424</count>
</prizeUnit>
<prizeUnit>
<string_ID>335CI</string_ID>
<prizeName>cashGrandPrize03</prizeName>
<count>0</count>
</prizeUnit>
</root_prizes>
我的上述文件的架构(我刚刚跳到架构中,如果其中有明显的问题,我深表歉意):
<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/XMLSchema.xsd"
xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:element name="root_prizes">
<xs:complexType>
<xs:sequence>
<!--https://stackoverflow.com/questions/25409242/creating-an-xml-document-with-more-than-1-child-element-with-xml-schema-->
<xs:element name="prizeUnit" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="string_ID" type="xs:string" />
<xs:element name="prizeName" type="xs:string" />
<xs:element name="count" type="xs:integer" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
用户输入是一个字符串,应该对应于“string_ID”,即“335AI”。
对于案例 #3,我遇到错误:未处理的异常:System.Xml.XPath.XPathException:表达式必须计算为节点集。
感谢您的任何帮助。
解决方案
在 XPath文档的帮助下,我设法为您构建了一个解决方案
//if you have non-empty xmlns tag you have to use namespace manager
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("ps", "http://tempuri.org/XMLSchema.xsd");
string id = "334BI";
XmlNode node = doc.SelectSingleNode($"/ps:root_prizes/ps:prizeUnit[ps:string_ID='{id}']", nsmgr);
//another of possible solutions
//XmlNode node = doc.SelectSingleNode($"descendant::ps:prizeUnit[ps:string_ID='{id}']", nsmgr);
如果您有一个空xmlns
标签或根本没有标签,以下解决方案也可以使用
XmlNode node = doc.SelectSingleNode($"/root_prizes/prizeUnit[string_ID='{id}']");
推荐阅读
- php - 从 localhost 到实时站点的 Curl 响应不一致
- c# - 如何使用不同的 chrome 配置文件 C#、selenium webdriver 并行触发多个 chrome 实例
- powershell - 有没有办法使用 powershell 打开到 cisco 路由器的 ssh 会话并运行 show 命令推断该信息?
- reactjs - 如何在 React 中将子组件状态传递给父组件状态
- docker - 为什么docker0需要混杂模式?
- rpmbuild - rpmbuild %install 部分删除 buildroot 代码。我如何防止 rpmbuild 这样做
- java - 如何使用spring data mongodb在GroupOperation中作为Id进行“substr”操作?
- python - ValueError: no json object could be decoded (Head First Python-chapter 8)
- nlp - Unigram 在语言识别方面比 Ngram 提供更好的结果
- php - 通过对公共值进行分组,将具有关联数组作为元素的索引数组转换为多维数组