Suponha que desejamos extrair alguns objetos de um arquivo xml usando C #.
o arquivo xml:
<Annotation text="Brazilian oil giant Petrobras and U.S. oilfield service company Halliburton have signed a technological cooperation agreement, Petrobras announced Monday.">
<Resources>
<Resource URI="http://dbpedia.org/resource/Oil" support="803" types="" surfaceForm="oil" offset="10" similarityScore="0.10925747454166412" percentageOfSecondRank="0.43448779313556946"/>
<Resource URI="http://dbpedia.org/resource/Company" support="427" types="" surfaceForm="company" offset="56" similarityScore="0.10083067417144775" percentageOfSecondRank="0.562207867961491"/>
</Resources>
</Annotation>
precisamos do par URI e surfaceForm do documento.
o código C # para fazer a análise:
using System.Xml.Linq
//some code here.
XDocument doc = XDocument.Parse("XmlFileName.xml");
var Resources = doc.Descendants("Resource").Select(resource => new
{
URI = resource.Attribute("URI").Value,
Entity = resource.Attribute("surfaceForm").Value,
}).ToList();
//to arrange the list of objects in a dictionary
Dictionary<string, Uri> entities = new Dictionary<string, Uri>();
foreach (var resource in Resources)
{
entities.Add(resource.Entity, new Uri(resource.URI));
}
note que resource.Attribute funcionará com os atributos do elemento xml, se você quiser que ele acesse os elementos, então resource.Element it is 🙂