Home > Net >  XPathSelectElements in a confusing XML file
XPathSelectElements in a confusing XML file

Time:07-08

This is silly but I am tapping out here guys...

I have this XML file, and I need to get the value of 2021 out of the following line: <A:product-version>2021</A:product-version>

And here is my full XML file

<?xml version="1.0" encoding="UTF-8"?>

-<entry xmlns:A="urn:schemas-autodesk-com:partatom" xmlns="http://www.w3.org/2005/Atom">

<title>REVIT_2021_PROJECT</title>

<updated>2022-07-06T09:56:13Z</updated>


-<A:taxonomy>

<term>adsk:revit</term>

<label>Autodesk Revit</label>

</A:taxonomy>


-<A:taxonomy>

<term>adsk:revit:grouping</term>

<label>Autodesk Revit Grouping</label>

</A:taxonomy>


-<link href="." type="application/rvt" rel="design-2d">


-<A:design-file>

<A:title>REVIT_2021_PROJECT.rvt</A:title>

<A:product>Revit</A:product>

<A:product-version>2021</A:product-version>

<A:updated>2022-07-06T09:56:13Z</A:updated>

</A:design-file>

</link>


-<A:features>


-<A:feature>

<A:title>Project Information</A:title>


-<A:group>

<A:title>Identity Data</A:title>

</A:group>


-<A:group>

<A:title>Energy Analysis</A:title>

</A:group>


-<A:group>

<A:title>Route Analysis</A:title>

</A:group>


-<A:group>

<A:title>Other</A:title>

<Project_Issue_Date type="system" typeOfParameter="Text" displayName="Project Issue Date">Issue Date</Project_Issue_Date>

<Project_Status type="system" typeOfParameter="Text" displayName="Project Status">Project Status</Project_Status>

<Client_Name type="system" typeOfParameter="Text" displayName="Client Name">Owner</Client_Name>

<Project_Address type="system" typeOfParameter="Multiline Text" displayName="Project Address">Enter address here</Project_Address>

<Project_Name type="system" typeOfParameter="Text" displayName="Project Name">Project Name</Project_Name>

<Project_Number type="system" typeOfParameter="Text" displayName="Project Number">Project Number</Project_Number>

</A:group>

</A:feature>

</A:features>

</entry>

And here is my c# code

        XDocument XprojectXMLfile = XDocument.Load(projectXMLfilePath);

        var nsMgr = new XmlNamespaceManager(new NameTable());
        nsMgr.AddNamespace("A", "urn:schemas-autodesk-com:partatom");

        var revitVersion = XprojectXMLfile.XPathSelectElements("/A:entry/A:link/A:design-file/A:product-version", nsMgr); 

I messed with the XPathSelectElements and namespaces but I have exhausted all my ideas and my brain is giving out :'C

CodePudding user response:

It seem to be that the elements not prefixed with an A: such as <link> element is in the document's default namespace, the Atom namespace, not the autodesk namespace. XPath 1.0 has no notion of a non-empty default namespace, so you will need to add a prefix to the namespace manager for Atom as well:

nsMgr.AddNamespace("atom", "http://www.w3.org/2005/Atom");

and qualify the core Atom elements in the XPath:

var revitVersion = XprojectXMLfile.XPathSelectElements("/atom:entry/atom:link/A:design-file/A:product-version", nsMgr);

  • Related