英文:
How to foreach following xml in c#
问题
List<Result> r = new List<Result>();
XDocument doc = XDocument.Parse(xmlResult);
foreach (var item in doc.Descendants("RESPONSE"))
{
r.Add(new Result
{
SearchValue = item.Element("QUERY1").Attribute("searched").Value,
SearchResult = item.Element("QUERY1").Value
});
r.Add(new Result
{
SearchValue = item.Element("QUERY2").Attribute("searched").Value,
SearchResult = item.Element("QUERY2").Value
});
r.Add(new Result
{
SearchValue = item.Element("QUERY3").Attribute("searched").Value,
SearchResult = item.Element("QUERY3").Value
});
r.Add(new Result
{
SearchValue = item.Element("QUERY4").Attribute("searched").Value,
SearchResult = item.Element("QUERY4").Value
});
r.Add(new Result
{
SearchValue = item.Element("QUERY5").Attribute("searched").Value,
SearchResult = item.Element("QUERY5").Value
});
r.Add(new Result
{
SearchValue = item.Element("QUERY6").Attribute("searched").Value,
SearchResult = item.Element("QUERY6").Value
});
r.Add(new Result
{
SearchValue = item.Element("QUERY7").Attribute("searched").Value,
SearchResult = item.Element("QUERY7").Value
});
}
这是你提供的代码的更新版本,用于将搜索键和搜索结果添加到 Result
对象的列表中。
英文:
I have following xml result
<?xml version="1.0" encoding="windows-1254" ?>
<RESPONSE>
<VALIDATION>1</VALIDATION>
<QUERY1 searched="12345">0</QUERY1>
<QUERY2 searched="aaaaa">2</QUERY2>
<QUERY3 searched="44444">2</QUERY3>
<QUERY4 searched="99999">0</QUERY4>
<QUERY5 searched="number">0</QUERY5>
<QUERY6 searched="bar">0</QUERY6>
<QUERY7 searched="foo">1</QUERY7>
</RESPONSE>
</xml>
I have following class
public class Result{
public string SearchValue {get;set;}
public string SearchResult {get;set;}
}
And this code
...
List<Result> r = new List<Result>();
XDocument doc = XDocument.Parse(xmlResult);
foreach( var item in doc.Descendants("RESPONSE")){
r.Add(new Result{
SearchValue = item.Attribute("searched").Value,
SearchResult = item.Element("QUERY?").Value
});
}
...
How to get all searched keys and searched result to list object?
答案1
得分: 2
I suspect you want something like:
XDocument doc = XDocument.Parse(xmlResult);
var results = doc.Root
.Elements()
.Where(e => e.Name.LocalName.StartsWith("QUERY"))
.Select(e => new Result {
SearchValue = (string) e.Attribute("searched"),
SearchResult = e.Value
})
.ToList();
That assumes that:
- You only care about elements that are direct children of the root element
- You want to ignore any elements whose local names don't start with "QUERY"
- You're okay with handling a missing
searched
attribute by creating aResult
object with a null value forSearchValue
- You're okay with
SearchResult
being the concatenation of all text nodes in the query element
英文:
I suspect you want something like:
XDocument doc = XDocument.Parse(xmlResult);
var results = doc.Root
.Elements()
.Where(e => e.Name.LocalName.StartsWith("QUERY"))
.Select(e => new Result {
SearchValue = (string) e.Attribute("searched"),
SearchResult = e.Value
})
.ToList();
That assumes that:
- You only care about elements that are direct children of the root element
- You want to ignore any elements whose local names don't start with "QUERY"
- You're okay with handling a missing
searched
attribute by creating aResult
object with a null value forSearchValue
- You're okay with
SearchResult
being the concatenation of all text nodes in the query element
答案2
得分: 1
你可以使用XPath选择降序元素,使用以下代码://RESPONSE/*[starts-with(name(),'QUERY')]
使用LINQ 重写后的代码如下:
var nodes = doc.SelectNodes("//RESPONSE/*[starts-with(name(),'QUERY')]")
.Cast<XmlNode>();
var query = (from elm in nodes
where elm.Attributes.Cast<XmlAttribute>().Any(x => x.Name == "searched")
select new Result()
{
SearchValue = elm.Attributes["searched"].Value,
SearchResult = elm.InnerText
}).ToList();
你可以在 dotnetfiddle.net 上查看示例。
英文:
You can select the descending elements using xpath //RESPONSE/*[starts-with(name(),'QUERY')]
foreach(XmlNode row in doc.SelectNodes("//RESPONSE/*[starts-with(name(),'QUERY')]"))
{
if(row.Attributes.Cast<XmlAttribute>().Any(x=>x.Name=="searched"))
{
Result r2 = new Result(){
SearchValue = row.Attributes["searched"].Value,
SearchResult = row.InnerText
};
r.Add(r2);
}
}
LINQ rewrite
var nodes = doc.SelectNodes("//RESPONSE/*[starts-with(name(),'QUERY')]")
.Cast<XmlNode>();
//.SelectMany(x=>x.ChildNodes.Cast<System.Xml.XmlElement>());
var query = (from elm in nodes
where elm.Attributes.Cast<XmlAttribute>().Any(x=>x.Name=="searched")
select new Result(){
SearchValue = elm.Attributes["searched"].Value,
SearchResult = elm.InnerText
}).ToList();
答案3
得分: 0
你可以将它转换成一个 JSON 对象并使用 Linq。
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlResult);
var jObj = JObject.Parse(JsonConvert.SerializeXmlNode(xmlDoc));
List<Result> results = ((JObject)jObj["RESPONSE"]).Properties()
.Where(v => v.Name.StartsWith("QUERY"))
.Select(v => new Result
{
SearchValue = (string)v.Value["@searched"],
SearchResult = (string)v.Value["#text"]
}).ToList();
英文:
you can convert it to a json object and use Linq
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlResult);
var jObj = JObject.Parse(JsonConvert.SerializeXmlNode(xmlDoc));
List<Result> results = ((JObject)jObj["RESPONSE"]).Properties()
.Where(v => v.Name.StartsWith("QUERY"))
.Select(v => new Result
{
SearchValue = (string)v.Value["@searched"],
SearchResult = (string)v.Value["#text"]
}).ToList();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论