gpt4 book ai didi

c# - 使用通用标记从 XML 中获取特定数据

转载 作者:太空宇宙 更新时间:2023-11-03 14:41:12 24 4
gpt4 key购买 nike

我发现的大多数教程都是基于实际上具有有用标签的 xml。我正在尝试根据集合中的一个条目获取一整套记录,使用具有非常通用标签的 XML。

//saved as thisxml.xml in current directory
//note the empty entry in the second row
/*
<?xml version="1.0" encoding="UTF-8"?>
<Note>
<ROW>
<ENT>up</ENT>
<ENT>Medium</ENT>
<ENT>Red</ENT>
<ENT>down</ENT>
<ENT>Medium</ENT>
</ROW>
<ROW>
<ENT>right</ENT>
<ENT>High</ENT>
<ENT>Purple</ENT>
<ENT/>
<ENT>High</ENT>
</ROW>
</Note>
*/
using System;
using System.Linq;
using System.Xml.Linq;

namespace ParseGenericXML
{
class Program
{
static void Main(string[] args)
{
XDocument document = XDocument.Load("thisxml.xml");
var selectRow = from r in document.Where(r=>(string)r.Element("ENT").Value=="Purple")
select new
{
value1 = r.Element("ENT").Value,
value2 = r.Element("ENT").Value,
value3 = r.Element("ENT").Value,
value4 = r.Element("ENT").Value,
value5 = r.Element("ENT").Value
};

//selectRow results should be {"right","High","Purple",null,"High"}
}
}
}

最佳答案

试试这个:

var selectRow =
from r in document.Root.Elements("ROW")
where r.Elements("ENT").Any(e => e.Value == "Purple")
select r.Elements("ENT").Select(e => e.Value).ToArray();

关于c# - 使用通用标记从 XML 中获取特定数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56814389/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com