I am trying to extract an element from an XDocument using the following:
private bool ContainsEntity(FileInfo filePath, string entityType, int id, string field, string value)
{
try{
var doc = XDocument.Load(filePath.FullName);
var entities = doc.Descendants(entityType);
var ns = XNamespace.Get("http://ift.tt/1zGmYi7");
var attr = new XAttribute(ns + "uuid", id);
var specificEntities = entities.Where(y =>
{
var atts = y.Attributes();
return atts.Contains(attr);
});
return specificEntities.Any(ent =>
ent.Elements(field).Any(f => f.Value.Equals(value, StringComparison.OrdinalIgnoreCase)));
}catch{return false;}
}
the element in question in the xml document looks like:
<depotStore sdata:uuid="4">
<operatingCompanyReference>1</operatingCompanyReference>
<originalType>
operatingCompanyWarehouse
</originalType>
<type>
operatingCompanyWarehouse
</type>
<name>warehouse4</name>
</depotStore>
the problem is that the attribute is not matched, so specificEntities is always a zero length collection. The problem appears to be with the namespace prefixing of the attribute name, but I cannot see how to initialize attr to the desired value.
No comments:
Post a Comment