I need some help with reading an oddly-formatted XML file. Because of the way the nodes and attributes are structured, I keep running into XMLException errors (at least, that’s what the output window is telling me; my breakpoints refuse to fire so that I can check it). Anyway, here’s the XML. Anyone experienced anything like this before?
<ApplicationMonitoring>
<MonitoredApps>
<Application>
<function1 listenPort="5000"/>
</Application>
<Application>
<function2 listenPort="6000"/>
</Application>
</MonitoredApps>
<MIBs>
<site1 location="test.mib"/>
</MIBs>
<Community value="public"/>
<proxyAgent listenPort="161" timeOut="2"/>
</ApplicationMonitoring>
Cheers
EDIT: Current version of the parsing code (file path shortened – Im not actually using this one):
XmlDocument xml = new XmlDocument();
xml.LoadXml(@"..\..\..\ApplicationMonitoring.xml");
string port = xml.DocumentElement["proxyAgent"].InnerText;
Your problem in loading the XML is that
xml.LoadXmlexpects you to pass the xml document as a string, not a file reference.Try instead using:
Essentially in your original code you are telling it that your xml document is
And I’m sure you can now see why there is a parse exception. 🙂 I’ve tested this with your xml document and the modified load and it works fine (except for the issue that Only Bolivian Here pointed out with the fact that your inner Text is not going to return anything.
For completeness you probably want:
Note the use of the Value property on the attribute and not the ToString method which won’t do what you are expecting.
Exactly how you extract the data from the xml is probably dependant on what you are doing with it. For example you may want to get a list of Application nodes to enumerate over with a foreach by doing this
xml.SelectNodes("//Application").If you are having trouble with extdacting stuff though that is probably the scope of a different question since this was just about how to get the XML document loaded.