C# 如何使用c从节点内部获取字符串的一部分#

C# 如何使用c从节点内部获取字符串的一部分#,c#,linq-to-xml,C#,Linq To Xml,我有一个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</pric

我有一个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>

马修·甘巴德拉
XML开发人员指南[49-o]
电脑类
44.95
2000-10-01
深入了解[41-p]使用XML创建应用程序。
拉尔斯,金
夜雨
幻想
5.95
2000-12-16
一位前建筑师[100-x]与企业僵尸、邪恶女巫以及自己的童年斗争,成为世界女王。
科雷茨,伊娃
梅夫上升
幻想
5.95
2000-11-17
在英国的纳米技术学会崩溃之后,[01-i]年轻的幸存者为新社会奠定了基础。

如何使用linq2xml从每个节点
提取值“[(\d+)-([a-z])”,并将其存储在变量中,或者像将提取的值添加到相应节点的新属性中那样使用它,如
等?

您可以使用
子体

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");

我不熟悉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();

谢谢,但是我如何使用这些值,比如将它们添加到节点属性中,比如说“id”…即
深入了解[41-p]使用XML创建应用程序。
应该是深入了解[41-p]使用XML创建应用程序。如何保存更改?下面的代码不执行任何操作
Regex Regex=new Regex(@“(\d+)-([a-z]);var xml=File.ReadAllText(@“C:\Users\Desktop\tttttt.xml”);var xdoc=XDocument.Parse(xml);var descriptions=xdoc.substands(“description”)。其中(x=>regex.Match(x.Value.Success);foreach(描述中的变量描述){var regexResult=regex.Match(description.Value).Value;var属性=new XAttribute(“id”,regexResult);description.Add(attribute);}File.writealText(@“C:\Users\Desktop\tttttt.xml”,xml)如何在不使用Xpath编写完整的节点树的情况下直接到达所需的节点?