Let's take an example of XML
as below to fetch data of document using XPath
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE svg (View Source for full doctype...)> <!-- Created with AIM. --> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1668.75 1074.75" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="xMidYMid meet" zoomAndPan="magnify" version="1.0" contentScriptType="text/ecmascript" contentStyleType="text/css"> can't read ": no such element in array <g id="#1" class="track" /> <g id="#5" class="dedication"> <metadata> <meta name="color">Red</meta> </metadata> <text fill="#181818">AQWSD</text> </g> <g id="#6" class="wordasword"> <metadata> <meta name="epigraph">Output 1</meta> <meta name="color">Red</meta> <meta name="refentry">qandadiv</meta> </metadata> <paramdef fill="none" /> <text fill="#181818">0.35</text> </g> <g id="#7" class="wordasword"> <metadata> <meta name="epigraph">Output 2</meta> <meta name="color">Red</meta> <meta name="refentry">calloutlist</meta> <meta name="screen">common></meta> </metadata> <path fill="none" /> <text fill="#181818">lineannotation</text> <text fill="#181818">WHO</text> <paramdef fill="#232323" /> </g> <g id="#" class="wordasword"> <metadata> <meta name="epigraph">Output 3</meta> <meta name="color">Red</meta> <meta name="refentry">calloutlist</meta> <meta name="screen">common></meta> </metadata> <path fill="none" /> <text fill="#181818">lineannotation</text> <text fill="#181818">WHO</text> <paramdef fill="#232323" /> </g> </svg>
I have checked and build the code below that is working correctly.
Below is run-time value of above mentioned XML document as xmlContent
.
var xmlContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG
1.0//EN\" \"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">\r\n<!--Created with AIM.-->\r\n<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 1668.75 1074.75\">\r\ncan't read \": no such element in array<g id=\"#1\" class=\"track\"></g><g id=\"#5\" class=\"dedication\">\r\n<metadata>\r\n<meta name=\"color\">Red</meta>\r\n</metadata>\r\n<text fill=\"#181818\">AQWSD</text>\r\n</g>\r\n<g id=\"#6\" class=\"wordasword\">\r\n<metadata>\r\n<meta name=\"epigraph\">Output 1</meta>\r\n<meta name=\"color\">Red</meta>\r\n<meta name=\"refentry\">qandadiv</meta>\r\n</metadata>\r\n<paramdef fill=\"none\" />\r\n<text fill=\"#181818\">0.35</text>\r\n</g>\r\n<g id=\"#7\" class=\"wordasword\">\r\n<metadata>\r\n<meta name=\"epigraph\">Output 2</meta>\r\n<meta name=\"color\">Red</meta>\r\n<meta name=\"refentry\">calloutlist</meta>\r\n<meta name=\"screen\">common></meta>\r\n</metadata>\r\n<path fill=\"none\" />\r\n<text fill=\"#181818\">lineannotation</text>\r\n<text fill=\"#181818\">WHO</text>\r\n<paramdef fill=\"#232323\"/>\r\n</g>\r\n<g id=\"#\" class=\"wordasword\">\r\n<metadata>\r\n<meta name=\"epigraph\">Output 3</meta>\r\n<meta name=\"color\">Red</meta>\r\n<meta name=\"refentry\">calloutlist</meta>\r\n<meta name=\"screen\">common></meta>\r\n</metadata>\r\n<path fill=\"none\"/>\r\n<text fill=\"#181818\">lineannotation</text>\r\n<text fill=\"#181818\">WHO</text>\r\n<paramdef fill=\"#232323\"/>\r\n</g>\r\n</svg>";
XmlDocument xml = new XmlDocument();
xml.LoadXml(xmlContent);
//Select all g Nodes of class wordasword that have color red in metadata>meta
var gNodesOnClassOfColorRed = xml.SelectNodes("//*[local-name()='g'][@class='wordasword'][*[local-name()='metadata'][*[local-name()='meta'][@name='color'] = 'Red']]").Cast<XmlNode>();
foreach (XmlNode gNode in gNodesOnClassOfColorRed)
{
var metadata = gNode.SelectSingleNode("*[local-name()='metadata']").Cast<XmlNode>(); //Fetch metadata of g tag
//Fetch epigraph value from meta tag from metadata
var epigraph = metadata.Cast<XmlNode>()
.Where(z => z.Attributes.Count != 0 && z.Attributes.GetNamedItem("name") != null && z.Attributes.GetNamedItem("name").Value.Trim().ToLower() == "epigraph")
.Select(p => p.InnerText).FirstOrDefault();
Console.WriteLine(epigraph);
}
The above code will fetch the epigraph
value from Metadata
. The output of the epigraph
value will be printed as
Output 1, Output 2, Output 3
The below code will fetch the text tag
list of all g tags
where is xml
is same as above
var elementList = (XmlNodeList)xml.SelectNodes("//*[local-name()='g'][@class='wordasword'][*[local-name()='text']]");
foreach (XmlNode xmlNode in elementList) //g
{
XmlNodeList textList = (XmlNodeList)xmlNode.SelectNodes("*[local-name()='text']");
}