简体   繁体   English

C#LINQ到XML

[英]c# linq to xml

I have an xml string that I wish to traverse using LINQ to XML (I have never used this, so wish to learn). 我有一个希望使用LINQ to XML遍历的xml字符串(我从未使用过,所以希望学习)。 However when I try to use 但是当我尝试使用

XDocument xDoc = XDocument.Load(adminUsersXML);
        var users = from result in xDoc.Descendants("Result")
                    select new
                    {
                        test = result.Element("USER_ID").Value
                    };

I get an error message saying illegal characters in path. 我收到一条错误消息,指出路径中包含非法字符。 reading up on it, it's because I cannot pass a standard string in this way. 阅读它,这是因为我不能以这种方式传递标准字符串。 Is there a way to use XML LINQ qith a standard string? 有没有办法使用XML LINQ qith标准字符串?

Thanks. 谢谢。

My guess is that adminUsersXML is the XML itself rather than a path to a file containing XML. 我的猜测是adminUsersXML本身就是XML,而不是包含XML的文件的路径。 If that's the case, just use: 如果是这种情况,请使用:

XDocument doc = XDocument.Parse(adminUsersXML);

MSDN中所述,您必须使用Parse函数从字符串创建XDocument。

我认为adminUserXML不是文件,而是包含xml的字符串,应该对其进行解析以使用XDocument.Parse(adminUserXML)转换为XDocument。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM