将 XML 读取到字典中

发布于 2024-11-18 11:48:32 字数 287 浏览 4 评论 0原文

我需要将 XML 文件读取到字典中。

我读了一些指南,只是对我不理解的奇怪单词(例如节点、XML 验证等)感到困惑。 那么,您能帮我介绍一下吗?

我有一个以这种格式编写的 XML 文件:

<database>
    <def number="1" name="one"/>
    <def number="2" name="two"/>
</database>

如前所述,我想将其存储在字典中。我该怎么办呢?

I need to read an XML file to a dictionary.

I read few guides and I only got confused from weird words that I don't understand (such as nodes, XML validation etc.).
So, could you please walk me through?

I have an XML file which is written in this format:

<database>
    <def number="1" name="one"/>
    <def number="2" name="two"/>
</database>

As mentioned, I want to store it in a dictionary. How would I go about that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

寒江雪… 2024-11-25 11:48:32
var data = XElement.Parse(xml)
    .Elements("def")
    .ToDictionary(
        el => (int)el.Attribute("number"),
        el => (string)el.Attribute("name")
    );

此操作:

  • 将 xml 解析为 XElement(从 开始)
  • 迭代 元素
  • 表单一个字典,使用 @number 作为键(解释为 int),使用 @name 作为值(作为字符串)
  • 分配此字典到数据,隐式类型为 Dictionary
var data = XElement.Parse(xml)
    .Elements("def")
    .ToDictionary(
        el => (int)el.Attribute("number"),
        el => (string)el.Attribute("name")
    );

This:

  • parses the xml into an XElement (starting at <database>)
  • iterates over the <def ...> elements
  • forms a dictionary, using @number as the key (interpreting as an int), and @name as the value (as a string)
  • assigns this dictionary to data, which is implicitly typed as Dictionary<int,string>
迷爱 2024-11-25 11:48:32

你的问题很基本,但也并非不恰当。不用担心。我会解释你应该做什么。

首先,您必须加载此 XML 文件(如果它在磁盘上)。否则你不需要这一步

XDocument database = XDocument.Load(pathToYourXmlFile);

到这里,你得到:

<database>
    <def number="1" name="one"/>
    <def number="2" name="two"/>
</database>

然后你必须获得所有 def 元素的列表:

List<XElement> defs = database.Elements("def");

到这里,你得到:

<def number="1" name="one"/>
<def number="2" name="two"/>

现在,你应该得到list(defs 中的每个 def):

foreach(XElement def in defs)
{
    // Here you have each def <def number="x" name="y" />
    int number = def.Attribute("number").value;
    string name = def.Attribute("name").value;
}

从每个 def 中提取信息的代码是:

int number = def.Attribute("number").value;
string name = def.Attribute("name").value;

现在您已经有了号码和姓名,只需将其添加到字典中即可。

dictionary.Add(number, name);

希望有帮助。

Your question is basic, but not inappropriate. Don't worry. I'll explain what you should do.

first you have to load this XML file (if it's on the disk). Otherwise you don't need this step

XDocument database = XDocument.Load(pathToYourXmlFile);

up to here, you got:

<database>
    <def number="1" name="one"/>
    <def number="2" name="two"/>
</database>

Then you have to get a list of all def elements:

List<XElement> defs = database.Elements("def");

up to here, you got:

<def number="1" name="one"/>
<def number="2" name="two"/>

Now, you should get each item of the list (each def in defs):

foreach(XElement def in defs)
{
    // Here you have each def <def number="x" name="y" />
    int number = def.Attribute("number").value;
    string name = def.Attribute("name").value;
}

the code to extract information from each def is:

int number = def.Attribute("number").value;
string name = def.Attribute("name").value;

Now that you have your number and name, just add it to your dictionary.

dictionary.Add(number, name);

Hope that helps.

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