Select Page
This entry has been published on 2018-10-23 and may be out of date.

Last Updated on 2018-10-23.

[:en]Taking the first steps with .NET XDocument objects, you might encounter everything you try to select from the XML will be NULL.

In this case you have to take a look at the namespaces.

Methods like Descendants() seem to always need the exact namespace written before e.g. the XML tag you are searching for.

Example: Reading and modifying an SVG image

var svg = XDocument.Parse(myImageSvgXmlText);
var ns = svg.Root.GetDefaultNamespace();

//search for a special tag called "<g>"
var frame = svg.Root.Descendants(ns + "g").Where(o => (string)o.Attribute("id") == "myFrame_Color").First();

//search for "<polygon>" tag
var pol = frame.Descendants(ns + "polygon").First();
pol.Attribute("style").Value += "; fill:" + myHexCode;

 

Advanced: Get and edit the attributes of an “<xlink-href> attribute” (referring to an internal or external binary/rasterized image)

XNamespace xLinkNS = "http://www.w3.org/1999/xlink";

imgTag.Attribute(xLinkNS + "href").Value = "data:image/png;base64," + myBase64png;

 

 [:]