如何读取 XML 表单中的多个 CodeTable
我正在处理的 XML 底部有多个代码表,我想查找来自 XML 顶部部分的代码,并从 XML 底部的表中获取 CodeText。
这里有两个代码表:
<CodeTable Name="Codes - Days of Week">
<CodeText CodeValue="" />
<CodeText CodeValue="1">SUNDAY</CodeText>
<CodeText CodeValue="2">MONDAY</CodeText>
<CodeText CodeValue="3">TUESDAY</CodeText>
<CodeText CodeValue="4">WEDNESDAY</CodeText>
<CodeText CodeValue="5">THURSDAY</CodeText>
<CodeText CodeValue="6">FRIDAY</CodeText>
<CodeText CodeValue="7">SATURDAY</CodeText>
</CodeTable>
<CodeTable Name="Codes - Bus Type">
<CodeText CodeValue="" />
<CodeText CodeValue="0">NOT A BUS</CodeText>
<CodeText CodeValue="1">SCHOOL (PUBLIC OR PRIVATE)</CodeText>
<CodeText CodeValue="2">TRANSIT</CodeText>
<CodeText CodeValue="3">INTERCITY</CodeText>
<CodeText CodeValue="4">CHARTER</CodeText>
<CodeText CodeValue="5">OTHER</CodeText>
</CodeTable>
我能够使用以下代码行来访问代码表:
string CodeTableName = "Codes - Days of Week";
XmlNode CodeTableNode = doc.SelectSingleNode("//CodeTable[@Name=\"" + Convert.ToString(CodeTableName) + "\"]");
我已经能够使用以下代码来获取总线类型 = 0 的代码文本:
XmlNode CodeTextNode = doc.SelectSingleNode("//CodeText[@CodeValue=\"" + Convert.ToString(BusCode) + "\"]");
code_text = CodeTextNode.InnerText;
Console.WriteLine(code_text);
我希望能够要做的是以某种方式将这两者放在一起吗?这样我就可以找到我需要的 CodeTable 然后根据CodeValue拉取正确的CodeText。
I have multiple Code Tables at the bottom of the XML that I am processing and I would like to lookup a code that comes from the top portion of the XML and get the CodeText from a table at the bottom of the XML.
Here are two of the CodeTables:
<CodeTable Name="Codes - Days of Week">
<CodeText CodeValue="" />
<CodeText CodeValue="1">SUNDAY</CodeText>
<CodeText CodeValue="2">MONDAY</CodeText>
<CodeText CodeValue="3">TUESDAY</CodeText>
<CodeText CodeValue="4">WEDNESDAY</CodeText>
<CodeText CodeValue="5">THURSDAY</CodeText>
<CodeText CodeValue="6">FRIDAY</CodeText>
<CodeText CodeValue="7">SATURDAY</CodeText>
</CodeTable>
<CodeTable Name="Codes - Bus Type">
<CodeText CodeValue="" />
<CodeText CodeValue="0">NOT A BUS</CodeText>
<CodeText CodeValue="1">SCHOOL (PUBLIC OR PRIVATE)</CodeText>
<CodeText CodeValue="2">TRANSIT</CodeText>
<CodeText CodeValue="3">INTERCITY</CodeText>
<CodeText CodeValue="4">CHARTER</CodeText>
<CodeText CodeValue="5">OTHER</CodeText>
</CodeTable>
I am able to use the following lines of code to get to the code table:
string CodeTableName = "Codes - Days of Week";
XmlNode CodeTableNode = doc.SelectSingleNode("//CodeTable[@Name=\"" + Convert.ToString(CodeTableName) + "\"]");
And I have been able to use the following to get the CodeText for Bus Type = 0:
XmlNode CodeTextNode = doc.SelectSingleNode("//CodeText[@CodeValue=\"" + Convert.ToString(BusCode) + "\"]");
code_text = CodeTextNode.InnerText;
Console.WriteLine(code_text);
What I would like to be able to do is put both of these together somehow? So I can get to the CodeTable I need
and then pull the correct CodeText based on the CodeValue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用 LinqToXml,您可以执行以下操作:
在生产代码中,我将确保在最后一次
.Value
调用之前首先检查null
。但除此之外这应该可行。If you use LinqToXml you could do the following:
In productive code I'd make sure that you first check for
null
before the last.Value
call. But otherwise this should work.