如何读取 XML 表单中的多个 CodeTable

发布于 2024-11-04 15:27:50 字数 1622 浏览 5 评论 0原文

我正在处理的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

永言不败 2024-11-11 15:27:50

如果您使用 LinqToXml,您可以执行以下操作:

using System.Linq;
using System.Xml.Linq;

// [...]
var codeKey = "0";
var name = "Codes - Bus Type";

var doc = XDocument.Load("bla");
var code = doc.Descendants().Where(
        d => d.Name == "CodeText" &&
        d.Parent.Attribute("Name").Value == name &&
        d.Attribute("CodeValue").Value == codeKey).FirstOrDefault().Value;

在生产代码中,我将确保在最后一次 .Value 调用之前首先检查 null 。但除此之外这应该可行。

If you use LinqToXml you could do the following:

using System.Linq;
using System.Xml.Linq;

// [...]
var codeKey = "0";
var name = "Codes - Bus Type";

var doc = XDocument.Load("bla");
var code = doc.Descendants().Where(
        d => d.Name == "CodeText" &&
        d.Parent.Attribute("Name").Value == name &&
        d.Attribute("CodeValue").Value == codeKey).FirstOrDefault().Value;

In productive code I'd make sure that you first check for null before the last .Value call. But otherwise this should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文