Tom Rider
Tom Rider

Reputation: 2815

How to access the child element from xml structure?

I have this following vary basic svg document with 1 flowtext and 1 text element :

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->

<svg
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:cc="http://creativecommons.org/ns#"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns="http://www.w3.org/2000/svg"
   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
   width="744.09448819"
   height="1052.3622047"
   id="svg2"
   version="1.1"
   inkscape:version="0.48.2 r9819"
   sodipodi:docname="New document 1">
  <defs
     id="defs4" />
  <sodipodi:namedview
     id="base"
     pagecolor="#ffffff"
     bordercolor="#666666"
     borderopacity="1.0"
     inkscape:pageopacity="0.0"
     inkscape:pageshadow="2"
     inkscape:zoom="0.35"
     inkscape:cx="375"
     inkscape:cy="520"
     inkscape:document-units="px"
     inkscape:current-layer="layer1"
     showgrid="false"
     inkscape:window-width="1366"
     inkscape:window-height="706"
     inkscape:window-x="-8"
     inkscape:window-y="-8"
     inkscape:window-maximized="1" />
  <metadata
     id="metadata7">
    <rdf:RDF>
      <cc:Work
         rdf:about="">
        <dc:format>image/svg+xml</dc:format>
        <dc:type
           rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
        <dc:title></dc:title>
      </cc:Work>
    </rdf:RDF>
  </metadata>
  <g
     inkscape:label="Layer 1"
     inkscape:groupmode="layer"
     id="layer1">
    <flowRoot
       xml:space="preserve"
       id="flowRoot2985"
       style="fill:black;stroke:none;stroke-opacity:1;stroke-width:1px;stroke-linejoin:miter;stroke-linecap:butt;fill-opacity:1;font-family:Arial;font-style:normal;font-weight:normal;font-size:40px;line-height:125%;letter-spacing:0px;word-spacing:0px;-inkscape-font-specification:Arial;font-stretch:normal;font-variant:normal;text-anchor:middle;text-align:center;writing-mode:lr">
      <flowRegion
         id="flowRegion2987">
         <rect
           id="rect2989"
           width="600"
           height="162.85715"
           x="97.14286"
           y="89.505043"
           style="-inkscape-font-specification:Arial;font-family:Arial;font-weight:normal;font-style:normal;font-stretch:normal;font-variant:normal;font-size:40px;text-anchor:middle;text-align:center;writing-mode:lr;line-height:125%" />
       </flowRegion>
      <flowPara
         id="flowPara2993">Yahoo</flowPara>
    </flowRoot> 


    <text
       xml:space="preserve"
       style="font-size:40px;font-style:normal;font-weight:normal;line-height:125%;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;font-family:Sans"
       x="197.14285"
       y="352.36218"
       id="text3003"
       sodipodi:linespacing="125%">
      <tspan
         sodipodi:role="line"
         id="tspan3005"
         x="197.14285"
         y="352.36218">Gmail</tspan>
    </text>
  </g>
</svg>

I just want to get the elements which is inside the g element (.i.e flowRoot & text ),

for this i tried :

XElement svg = XElement.Load("path to svg file");
IEnumerable<XElement> elements = svg.Element("g").Elements();

But this is not working. Help me out please?

Upvotes: 3

Views: 1396

Answers (4)

Ivan Golović
Ivan Golović

Reputation: 8832

Try this, your XML uses namespaces:

        XDocument doc = XDocument.Load(<path to file>);
        XNamespace ns1 = "http://www.w3.org/2000/svg";
        //Namespace of a root element can also be retrieved like this:
        //XNamespace ns1 = doc.Root.GetDefaultNamespace();
        var g = doc.Descendants(ns1 + "g").FirstOrDefault();
        if (g != null)
        {
            var flowroot = g.Element(ns1 + "flowRoot");
            var text = g.Element(ns1 + "text");
        }

You should have prefixed name with a namespace:

        IEnumerable<XElement> elements = svg.Element(ns1 + "g").Elements();

Upvotes: 2

Chuck Savage
Chuck Savage

Reputation: 11955

Here, (using this Xml Library)

IEnumerable<XElement> elements = svg.XPath("//g/*");

That returns the two XElement's within. But if there is more than 1 g and you want just the first one, use "//g[1]/*".

See this example, click Test to run the test. After you test that, remove the [1] to see that the results include the next g, so you can see its important whether there is more than 1 g for getting the results you want.

Upvotes: 0

stevethethread
stevethethread

Reputation: 2524

You can use XPath with xdoc in the following way.

var nodes = svg.XPathSelectElements(path);

This will give your g nodes, then you can query the children of these using .Element("") or .Elements(""), depending on where you are expecting multiple or single child nodes.

Upvotes: 0

Ria
Ria

Reputation: 10357

If you not consist on Linq2Xml you can use XmlDocument and XPath:

var doc = new XmlDocument();
doc.Load(fileName);

var nodeList = doc.SelectNodes("//g");
if(nodeList != null)
    foreach (XmlNode node in nodeList)
    {
        // do anything you like
    }

explain:

SelectNodes(String) Selects a list of nodes matching the XPath expression.

//g in XPath select all elements in the document.

Upvotes: 0

Related Questions