LINQ-to-XML 中的 InnerText 相当于什么?

发布于 2024-08-10 01:05:51 字数 430 浏览 2 评论 0原文

我的 XML 是:

<CurrentWeather>
  <Location>Berlin</Location>
</CurrentWeather>

我想要字符串“Berlin”,如何从元素 Location 中获取内容,例如 InnerText

XDocument xdoc = XDocument.Parse(xml);
string location = xdoc.Descendants("Location").ToString(); 

以上返回

System.Xml.Linq.XContainer+d__a

My XML is:

<CurrentWeather>
  <Location>Berlin</Location>
</CurrentWeather>

I want the string "Berlin", how do get contents out of the element Location, something like InnerText?

XDocument xdoc = XDocument.Parse(xml);
string location = xdoc.Descendants("Location").ToString(); 

the above returns

System.Xml.Linq.XContainer+d__a

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

吹泡泡o 2024-08-17 01:05:51

对于您的特定示例:

string result = xdoc.Descendants("Location").Single().Value;

但是,请注意,如果您有更大的 XML 示例,后代可以返回多个结果:

<root>
 <CurrentWeather>
  <Location>Berlin</Location>
 </CurrentWeather>
 <CurrentWeather>
  <Location>Florida</Location>
 </CurrentWeather>
</root>

上述代码将更改为:

foreach (XElement element in xdoc.Descendants("Location"))
{
    Console.WriteLine(element.Value);
}

For your particular sample:

string result = xdoc.Descendants("Location").Single().Value;

However, note that Descendants can return multiple results if you had a larger XML sample:

<root>
 <CurrentWeather>
  <Location>Berlin</Location>
 </CurrentWeather>
 <CurrentWeather>
  <Location>Florida</Location>
 </CurrentWeather>
</root>

The code for the above would change to:

foreach (XElement element in xdoc.Descendants("Location"))
{
    Console.WriteLine(element.Value);
}
情愿 2024-08-17 01:05:51
public static string InnerText(this XElement el)
{
    StringBuilder str = new StringBuilder();
    foreach (XNode element in el.DescendantNodes().Where(x=>x.NodeType==XmlNodeType.Text))
    {
        str.Append(element.ToString());
    }
    return str.ToString();
}
public static string InnerText(this XElement el)
{
    StringBuilder str = new StringBuilder();
    foreach (XNode element in el.DescendantNodes().Where(x=>x.NodeType==XmlNodeType.Text))
    {
        str.Append(element.ToString());
    }
    return str.ToString();
}
女皇必胜 2024-08-17 01:05:51
string location = doc.Descendants("Location").Single().Value;
string location = doc.Descendants("Location").Single().Value;
往事风中埋 2024-08-17 01:05:51
string location = (string)xdoc.Root.Element("Location");
string location = (string)xdoc.Root.Element("Location");
情独悲 2024-08-17 01:05:51

在单个元素的简单情况下,

<CurrentWeather>
   <Location>Berlin</Location>
</CurrentWeather>

string location = xDocument..Descendants("CurrentWeather").Element("Location").Value;

像这样的多个元素,

<CurrentWeather>
    <Location>Berlin</Location>
    <Location>Auckland</Location>
   <Location>Wellington</Location>
</CurrentWeather>

  foreach (var result in xDocument.Descendants("CurrentWeather").Select(x => new{ location = x.Element("Location").Value.Tostring() }))
        {
          Locations + = location ;
        }

In a simple case with the single element,

<CurrentWeather>
   <Location>Berlin</Location>
</CurrentWeather>

string location = xDocument..Descendants("CurrentWeather").Element("Location").Value;

Multiple elements like this,

<CurrentWeather>
    <Location>Berlin</Location>
    <Location>Auckland</Location>
   <Location>Wellington</Location>
</CurrentWeather>

  foreach (var result in xDocument.Descendants("CurrentWeather").Select(x => new{ location = x.Element("Location").Value.Tostring() }))
        {
          Locations + = location ;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文