Picking off XML Elements

  • Thread starter Thread starter chance
  • Start date Start date
C

chance

If i have this XML schema:


<col>foo</foo>
<val>123</val>


I would like to write a function (or use existing .NET functionality)
to return the value given the column name. That is to say, I would
like to do something like this:

int myVal = someFunction(foo)

This would return 123.

Any help appreciated.

tia,
chance.
 
That isn't really a schema, but some xpath involving "*[col='" + name
+ "']/val" and int.Parse on the InnerText would probably do the job.
Can't be more specific without something more complete.

Marc
 
That isn't really a schema, but some xpath involving "*[col='" + name
+ "']/val" and int.Parse on the InnerText would probably do the job.
Can't be more specific without something more complete.

Marc

Is this more helpful?

<?xml version="1.0" encoding="utf-8" ?>
- <parameters>
- <parm>
<col>notice_of_violation.notice_of_violation_id</col>
<val>A1000125</val>
</parm>
</parameters>
 
yup; note I've deliberately left this *very* crude... exercise for
reader to tidy it up ;-p

Marc

static void Main() {
string xml = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
<parameters>
<parm>
<col>notice_of_violation.notice_of_violation_id</col>
<val>A1000125</val>
</parm>
</parameters>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
Console.WriteLine(GetParam(doc,
"notice_of_violation.notice_of_violation_id"));

}
static string GetParam(XmlDocument doc, string key) {
return doc.SelectSingleNode("/parameters/parm[col='" + key
+ "']/val").InnerText;
}
 
yup; note I've deliberately left this *very* crude... exercise for
reader to tidy it up ;-p

Marc

static void Main() {
string xml = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
<parameters>
<parm>
<col>notice_of_violation.notice_of_violation_id</col>
<val>A1000125</val>
</parm>
</parameters>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
Console.WriteLine(GetParam(doc,
"notice_of_violation.notice_of_violation_id"));

}
static string GetParam(XmlDocument doc, string key) {
return doc.SelectSingleNode("/parameters/parm[col='" + key
+ "']/val").InnerText;
}

okay suppose the XML is in a file called C:\\XMLFile1.xml
 
chance said:
okay suppose the XML is in a file called C:\\XMLFile1.xml

Then instead of LoadXml use the Load method
XmlDocument doc = new XmlDocument();
doc.Load(@"C:\XMLFile1.xml");
 
Then instead of LoadXml use the Load method
XmlDocument doc = new XmlDocument();
doc.Load(@"C:\XMLFile1.xml");

Okay I'm getting this run-time error and I don't know why...

This is my function:
private string getParams(XmlDocument xmlDoc, string key)
{
string retVal;
retVal = xmlDoc.SelectSingleNode("/parameters/parm[col='" +
key + "']/val").InnerText;
return retVal;

}

This is the call:

//read XML and append
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("C:\\XMLFile1.xml");



string test = getParams(xmlDoc,
"notice_of_violation.notice_of_violation_id");

And this is the contents of the XML file:
<?xml version="1.0" encoding="utf-8" ?>
- <parameters>
- <parm>
<col>notice_of_violation.notice_of_violation_id</col>
<val>A1000125</val>
</parm>
</parameters>

This is the error:
System.NullReferenceException: Object reference not set to an instance
of an object.
at reportNOV.getParams(XmlDocument xmlDoc, String key) in c:
\Documents and Settings\dchance\My Documents\Visual Studio
2005\WebSites\DocServerII\App_Code\rptNOV.cs:line 33
at reportNOV.generateNOV(String xmlParms) in c:\Documents and
Settings\dchance\My Documents\Visual Studio 2005\WebSites\DocServerII
\App_Code\rptNOV.cs:line 65
at Service.buildDocument(String rptType, String accessKey, String
xmlParms) in c:\Documents and Settings\dchance\My Documents\Visual
Studio 2005\WebSites\DocServerII\App_Code\Service.cs:line 40
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top