I’m trying to read through an XML (Actually a GML, but I don’t think that makes a difference) and have been running into issues with parsing. This is my first brush with dealing in XML.
My code in C#
void ParseXML(String path)
{
XmlReader reader = XmlReader.Create( new FileStream( path, FileMode.Open ) );
reader.Read();
while( reader.Read() )
{
// Only detect start elements.
if ( reader.IsStartElement() && reader.Name == "gml:featureMember" )
{
Debug.Log(reader.Name);
reader.ReadToDescendant("gml:featureMember");
Debug.Log(reader["ogr:X"]);
}
}
}
A section from the GML I’m working with:
<gml:featureMember>
<ogr:fence_neighbors fid="F1">
<ogr:Name>Xyz Xyz</ogr:Name>
<ogr:X>2353.45361911000</ogr:X>
<ogr:Y>-4652.36641288000</ogr:Y>
</ogr:fence_neighbors>
</gml:featureMember>
Making this slightly more difficult is that I am working without access to System.Xml.Linq (Unity3D). I get reader.Name just fine, but reader[“ogr:X”] always comes up null. It seems like I’m not using ReadToDescendant and Reader properly, but I’m not quite sure where to start.
The
XmlReaderdoes not know about the namespace prefixes as you’re using them.Here is a test program that does what I think you’re after. Note that the namespace URI is given explicitly.
I added declarations to the source document for testing:
The output is simply