我有一个像
这样的xml文件<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide [49-o]</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at [41-p] creating applications with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect [100-x] battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description>
</book>
<book id="bk103">
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-11-17</publish_date>
<description>After the collapse of a nanotechnology society in England, the [01-i] young survivors lay the foundation for a new society.</description>
</book>
</catalog>
如何使用 linq2xml 来提取值&#34; [(\ d +) - ([a-z])]&#34;来自每个节点<description>
并将其存储在一个变量中,或者可以使用它将这些提取的值添加到各个节点的新属性中,如<description val="41-p">
等?
答案 0 :(得分:3)
您可以使用Descendants
Regex regex = new Regex(@"(\d+)-([a-z])");
var xdoc = XDocument.Parse(xml);
var descriptions = xdoc.Descendants("description")
.Where(x => regex.Match(x.Value).Success)
.Select(x => regex.Match(x.Value).Value).ToList();
Output:
41-p
100-x
01-i
如果要将提取的值设置为属性;
Regex regex = new Regex(@"(\d+)-([a-z])");
var xdoc = XDocument.Parse(xml);
var descriptions = xdoc.Descendants("description")
.Where(x => regex.Match(x.Value).Success);
foreach (var description in descriptions)
{
var regexResult = regex.Match(description.Value).Value;
var attribute = new XAttribute("id", regexResult);
description.Add(attribute);
}
xdoc.Save("sample.xml");
答案 1 :(得分:0)
我不熟悉linq2xml,所以我使用XmlDocument和XPath表达式找到我感兴趣的节点。这样的事情:
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlString);
var books = doc.SelectNodes("//catalog/book");
foreach (XmlNode book in books)
{
var description = book.SelectSingleNode("description");
Regex regex = new Regex(@"(\[.*\])");
var match = regex.Match(description.InnerText);
if (match.Success)
{
var val = match.Groups[0].Value;
var attribute = doc.CreateAttribute("val");
attribute.Value = val;
description.Attributes.SetNamedItem(attribute);
}
}
// Convert XmlDocument back to string
var stringWriter = new StringWriter();
var xmlTextWriter = XmlWriter.Create(stringWriter);
doc.WriteTo(xmlTextWriter);
xmlTextWriter.Flush();
xmlString = stringWriter.GetStringBuilder().ToString();