Mon XML est:
XDocument xdoc = XDocument.Parse(xml); string location = xdoc.Descendants("Location").ToString();
5 Réponses :
pour votre échantillon particulier: Toutefois, notez que les descendants peuvent renvoyer plusieurs résultats si vous aviez un échantillon XML plus grand: P> foreach (XElement element in xdoc.Descendants("Location"))
{
Console.WriteLine(element.Value);
}
J'avais essayé et j'avais eu une erreur sur célibataire (), je avais eu "à l'aide de system.xml.linq" mais j'ai oublié "à l'aide du système.linq", merci.
string location = doc.Descendants("Location").Single().Value;
string location = (string)xdoc.Root.Element("Location");
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(); }
Dans un cas simple avec l'élément unique, plusieurs éléments tels que celui-ci, P> <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 ;
}