Reputation: 127603
Here is a snippet of the XML file I am trying to read.
<?xml version="1.0" encoding="UTF-8" ?>
<FMPXMLRESULT xmlns="http://www.filemaker.com/fmpxmlresult">
<ERRORCODE>0</ERRORCODE>
<PRODUCT BUILD="06-11-2010" NAME="FileMaker" VERSION="Pro 11.0v2" />
<DATABASE DATEFORMAT="M/d/yyyy" LAYOUT="" NAME="Appointments.fp7" RECORDS="13" TIMEFORMAT="h:mm:ss a" />
<METADATA>
<FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="CalDateCalc1" TYPE="DATE" />
<FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="CalDateCalc2" TYPE="DATE" />
<FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="CalDateCalc3" TYPE="DATE" />
...
</METADATA>
...
</FMPXMLRESULT>
My goal is to get the the NAME
attribute and the TYPE
attribute out of the xml file. I have tried to use both XElement and XPath but I have gotten neither to work.
foreach (var file in System.IO.Directory.EnumerateFiles(path, "*.xml", System.IO.SearchOption.TopDirectoryOnly))
{
var fileInfo = new System.IO.FileInfo(file);
var name = fileInfo.Name.Split('.')[0];
XElement root = XElement.Load(file);
if (!db.Tables.Contains(name))
{
XElement MetaData = root.Element("METADATA"); //This returns null
foreach(var field in MetaData.Elements("FIELD"))
{
//...
}
}
//...
}
When I try to select the METADATA
group the selection returns null. What mistake am I making with my code?
Upvotes: 1
Views: 172
Reputation: 263047
Your <METADATA>
element resides in an XML namespace. You have to specify that namespace to the Element() method:
XNamespace ns = "http://www.filemaker.com/fmpxmlresult";
XElement metaData = root.Element(ns + "METADATA"); // This will work.
See the XName class for more details.
Upvotes: 1