I'm trying to get the value of a particular attribute of a sub-element in sub-elements of a particular value.
Sub-element = "text" and
Attribute = "transform"
I'm using XML.Linq. I looked at several tutorials including here: https://www.dreamincode.net/forums/topic/353127-linq-by-example-5-linq-to-xml-querying/
The IEnumerable XElement try1, try2, and try3 in the code below all remain null. The value that I'm trying to return is "matrix(0.19885 0 -0 0.19885 468.353 9352.33)"
Below is a sample XML and c# code:
XML in SVG file:
<svg viewBox="-4773 -7277 42423 24101" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" stroke-linecap="round" stroke-linejoin="round" fill-rule="evenodd" xml:space="preserve">
<g clip-path="url(#clipId0)" fill="rgb(100,100,100)" stroke="rgb(100,100,100)" stroke-width="0">
<text transform="matrix(0.19885 0 -0 0.19885 468.353 9352.33)" font-family="Verdana,'sans-serif'" font-size="1.37493">Test1 and other text</text>
<text transform="matrix(0.19885 0 -0 0.19885 567.778 9352.33)" font-family="Verdana,'sans-serif'" font-size="1.37493">Test2 and other text</text>
</g>
</svg>
c# code:
public string GetAttributeValue()
{
XElement xml_Elem = XElement.Load(_FilePath);
List<string> retVal = new List<string>;
IEnumerable<XElement> try1 =
from el in xml_Elem.Elements("g")
where el.Element("text").Value.Contains("Test1")
select el;
foreach (XElement el in try1)
{
retVal.Add(el.Attribute("transform").ToString());
}
IEnumerable<XElement> try2 =
from el in xml_Elem.Descendants("text")
where el.Value.Contains("Test1")
select el;
foreach (XElement el in try2)
{
retVal.Add(el.Attribute("transform").ToString());
}
IEnumerable<XAttribute> try3 =
from el in xml_Elem.Descendants("text")
where el.Value.Contains("Test1")
select el.Attribute("transforem");
foreach (XAttribute at in try3)
{
retVal.Add(at.Value);
}
return retVal;
}
The problem was with the namespaces. See duplicate post reference.