C# 如何使用Linq to XML解析XML字符串并查找以指定字母开头的值的所有节点?
给定一个XML文本字符串,如C# 如何使用Linq to XML解析XML字符串并查找以指定字母开头的值的所有节点?,c#,linq-to-xml,C#,Linq To Xml,给定一个XML文本字符串,如 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<directory dirname=\"c\">" + "<directory dirname=\"documents\">" + "<directory dirname=\"daves projects 2015\" />" +
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<directory dirname=\"c\">" +
"<directory dirname=\"documents\">" +
"<directory dirname=\"daves projects 2015\" />" +
"<directory dirname=\"vics projects 2015\" />" +
"<directory dirname=\"daves projects 2014\" />" +
"</directory>" +
"<directory dirname=\"daves projects archive\" />" +
"</directory>";
“”+
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"";
使用Linq to XML解析此字符串以返回以“dave”开头的目录名列表的当前首选方法是什么。还是有比使用Linq转换XML更好的方法
我需要一个字符串列表,如下所示
public static void Main(string[] args)
{
string xml =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<directory dirname=\"c\">" +
"<directory dirname=\"documents\">" +
"<directory dirname=\"daves projects 2015\" />" +
"<directory dirname=\"vics projects 2015\" />" +
"<directory dirname=\"daves projects 2014\" />" +
"</directory>" +
"<directory dirname=\"daves projects archive\" />" +
"</directory>";
var Folders = ParseXmlString(xml, "dave");
foreach (XElement name in Folders)
Console.WriteLine(name.Value);
}
private static List<XElement> ParseXmlString(string xmlStr, string searchValue)
{
List<XElement> result = new List<XElement>();
try
{
var str = XElement.Parse(xmlStr);
result = str.Elements("directory").Where(x => x.Element("dirname").Value.StartsWith(searchValue)).ToList();
}
catch (Exception ex)
{
throw ex;
}
return result;
}
2015年daves项目
daves项目2014
戴维项目档案馆
我试着使用以下方法
public static void Main(string[] args)
{
string xml =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<directory dirname=\"c\">" +
"<directory dirname=\"documents\">" +
"<directory dirname=\"daves projects 2015\" />" +
"<directory dirname=\"vics projects 2015\" />" +
"<directory dirname=\"daves projects 2014\" />" +
"</directory>" +
"<directory dirname=\"daves projects archive\" />" +
"</directory>";
var Folders = ParseXmlString(xml, "dave");
foreach (XElement name in Folders)
Console.WriteLine(name.Value);
}
private static List<XElement> ParseXmlString(string xmlStr, string searchValue)
{
List<XElement> result = new List<XElement>();
try
{
var str = XElement.Parse(xmlStr);
result = str.Elements("directory").Where(x => x.Element("dirname").Value.StartsWith(searchValue)).ToList();
}
catch (Exception ex)
{
throw ex;
}
return result;
}
publicstaticvoidmain(字符串[]args)
{
字符串xml=
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"";
var Folders=ParseXmlString(xml,“dave”);
foreach(文件夹中的元素名称)
Console.WriteLine(name.Value);
}
私有静态列表ParseXmlString(字符串xmlStr,字符串searchValue)
{
列表结果=新列表();
尝试
{
var str=XElement.Parse(xmlStr);
result=str.Elements(“目录”).Where(x=>x.Element(“dirname”).Value.StartsWith(searchValue)).ToList();
}
捕获(例外情况除外)
{
掷骰子;
}
返回结果;
}
但结果总是空的
有什么建议吗?如下更改功能
foreach (XAttribute name in Folders)
private static List<XAttribute> ParseXmlString(string xmlStr, string searchValue)
{
List<XAttribute> result = new List<XAttribute>();
try
{
XDocument doc = XDocument.Parse(xmlStr);
result = doc.Descendants("directory")
.Attributes("dirname")
.Where(x => x.Value.StartsWith("d"))
.ToList();
}
catch (Exception ex)
{
throw ex;
}
return result;
}
私有静态列表ParseXmlString(字符串xmlStr,字符串searchValue)
{
列表结果=新列表();
尝试
{
XDocument doc=XDocument.Parse(xmlStr);
结果=文档子体(“目录”)
.属性(“名称”)
其中(x=>x.Value.StartsWith(“d”))
.ToList();
}
捕获(例外情况除外)
{
掷骰子;
}
返回结果;
}
如果有帮助,请告诉我 如下更改功能
foreach (XAttribute name in Folders)
private static List<XAttribute> ParseXmlString(string xmlStr, string searchValue)
{
List<XAttribute> result = new List<XAttribute>();
try
{
XDocument doc = XDocument.Parse(xmlStr);
result = doc.Descendants("directory")
.Attributes("dirname")
.Where(x => x.Value.StartsWith("d"))
.ToList();
}
catch (Exception ex)
{
throw ex;
}
return result;
}
私有静态列表ParseXmlString(字符串xmlStr,字符串searchValue)
{
列表结果=新列表();
尝试
{
XDocument doc=XDocument.Parse(xmlStr);
结果=文档子体(“目录”)
.属性(“名称”)
其中(x=>x.Value.StartsWith(“d”))
.ToList();
}
捕获(例外情况除外)
{
掷骰子;
}
返回结果;
}
如果有帮助,请告诉我 在VB中,使用XElement
Dim xe As XElement = XElement.Parse(xml)
Dim ie As IEnumerable(Of String) = From el In xe...<directory>
Where el.@dirname.StartsWith("dave")
Select el.@dirname
Dim xe As XElement=XElement.Parse(xml)
Dim ie As IEnumerable(字符串)=从el到xe。。。
其中el@dirname.StartsWith(“dave”)
选择el名称
在VB中,使用XElement
Dim xe As XElement = XElement.Parse(xml)
Dim ie As IEnumerable(Of String) = From el In xe...<directory>
Where el.@dirname.StartsWith("dave")
Select el.@dirname
Dim xe As XElement=XElement.Parse(xml)
Dim ie As IEnumerable(字符串)=从el到xe。。。
其中el@dirname.StartsWith(“dave”)
选择el名称
您需要一个递归函数。平面函数不会返回目录层次结构中的值。您需要一个递归函数。平面函数不会返回目录层次结构中的值。它的工作方式很有魅力。感谢像一种魅力。谢谢