简体   繁体   English

从 xml 读取特定元素

[英]Reading specific element from xml

I feel this is really stupid but can't seem to figure it out.我觉得这真的很愚蠢,但似乎无法弄清楚。

I have a Combobox that populates from an XML file and works great.我有一个从 XML 文件填充的 Combobox,效果很好。 Now I want to display a specific element (Description) when an item is selected, but the string always wants to return null.现在我想在选择项目时显示特定元素(描述),但字符串总是想返回 null。

Xml: Xml:

<?xml version="1.0" encoding="utf-8" ?>
<Barbarian>
  <Special id="Lesser Ancestor Totem">
    <SpecialName>Lesser Ancestor Totem</SpecialName>
    <Description>Gain a +2 Insight Bonus to a skill (that you can use while raging) while raging</Description>
  </Special>
</Barbarian>

Code to get the description and put into a RichTextBox:获取描述并放入 RichTextBox 的代码:

 public void LoadFeatDescription()
        {
            string Class = CharClassSelector.Text;
            string Feat = FeatPicker.Text;

            XDocument doc = XDocument.Load($"\\Xml\\Classes\\{Class}.xml");

            string description = doc.XPathSelectElement($"//Special[@id='{Feat}']/Description").Value;
            //the string description wants to stay null despite my efforts
            DescriptionBox.Text = description;
                 
        }

The idea is that this will load a specific file and get the description element based on the id.这个想法是,这将加载一个特定的文件并根据 id 获取描述元素。 Am I missing something stupid?我错过了什么愚蠢的东西吗?

Thanks!谢谢!

Edit: added in the entire XML contents编辑:在整个 XML 内容中添加

I got it.我知道了。

Changed string description = doc.XPathSelectElement($"//Special[@id='{Feat}']/Description").Value;更改string description = doc.XPathSelectElement($"//Special[@id='{Feat}']/Description").Value; to string description = (string)doc.XPathSelectElement($"//Special[@id='{Feat}']/Description").Value; to string description = (string)doc.XPathSelectElement($"//Special[@id='{Feat}']/Description").Value;

I knew it was something stupid...我知道这是一件愚蠢的事情......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM