Medic3000
Medic3000

Reputation: 786

C#: Read XML Attribute

Using C#2.0 and VIsualStudio2005

I'm trying to access the "DisplayName" inside "MonitorResponseRecord" from an XML file like the one Below:

    <Magellan xsi:schemaLocation="http://tempuri.org/XMLSchema.xsd ..\Schema\Configuration.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd">
      <SchemaVersion>1.0</SchemaVersion>
          <MonitorScope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" id="CleanStationChemicalManifoldFeed5" xmlns="http://tempuri.org/XMLSchema.xsd">
            <PersonalSafety>
              <MonitorResponseRecord Enabled="true" DisplayName="ChemicalManifoldFeed5ControllerFault">
                <ExpressionMonitor>
                  <Expression>(ChemicalManifold.Feed5.DispenseValve = Open) and ((ChemicalManifold.Feed5.ViolatedRegion = HighProcess) or (ChemicalManifold.Feed5.ViolatedRegion = LowProcess) or (ChemicalManifold.Feed5.ViolatedRegion = Minimum))</Expression>
                  <TestInterval>0.1</TestInterval>
                  <MinimumTimeBetweenResponses>5</MinimumTimeBetweenResponses>
                </ExpressionMonitor>
                <Response>
                  <PostAlarm>
                    <AlarmName>ChemicalManifold_Feed5_ControllerFault</AlarmName>
                    <Parameter1 />
                    <Parameter2>Alarm Region = {ChemicalManifold.Feed5.ViolatedRegion}</Parameter2>
                    <Parameter3>{RecipeName}-{StepName}</Parameter3>
                    <Parameter4>FlowSetpoint = {ChemicalManifold.Feed5.Setpoint}-LPM, ActualFlow = {ChemicalManifold.Feed5.FlowMeter}-LPM</Parameter4>
                  </PostAlarm>
                  <ResponseEvent>
                    <TargetResource />
                    <Event>PA</Event>
                    <Reason>ChemicalSupply</Reason>
                  </ResponseEvent>
                </Response>
              </MonitorResponseRecord>
            </PersonalSafety>
            <PersonalSafety>
              <MonitorResponseRecord Enabled="true" DisplayName = "PressureValveFailure">
           ...
            ...                
             ...and soon

My current C# attempt is as follows, BUT it always hangs up when I try to XmlDocument.Load("");

                XmlDocument doc = new XmlDocument();
                doc.Load("../UMC0009.Configuration.Root.xml");
                string attrVal = doc.SelectSingleNode("MonitorResponseRecord/@DisplayName").Value;
                

BUUUUT won't work :/ any help out there?

UPDATE:

the exception I recieve is as follows, and occures at the doc.Load("...") line:

{"Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function."} System.Exception {System.Xml.XPath.XPathException}

Upvotes: 4

Views: 2585

Answers (2)

KrisG
KrisG

Reputation: 1091

Your XPath query will be relative to the document root:

doc.SelectSingleNode("MonitorResponseRecord/@DisplayName")

To make it search anywhere in the doc prefix it with double slash:

doc.SelectSingleNode("//MonitorResponseRecord/@DisplayName")

If that still doesn't work I would try the above example after stripping out all those namespace declarations on the two root nodes.

Otherwise, with the namespace declarations you may find you need to define XML namespace mappings and use prefixes in your XPath like:

XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("x", "http://tempuri.org/XMLSchema.xsd");

doc.SelectSingleNode("//x:MonitorResponseRecord/@DisplayName")

Upvotes: 5

Adolfo Perez
Adolfo Perez

Reputation: 2874

What about:

    XmlDocument doc = new XmlDocument();
    doc.Load("UMC0009.Configuration.Root.xml");

    XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
    nsmgr.AddNamespace("ns", "http://tempuri.org/XMLSchema.xsd");
    string attrVal = doc.SelectSingleNode("//ns:MonitorResponseRecord/@DisplayName", nsmgr).Value;

Using a namespace manager, specify your namespace URI and use it in your XPath. It works for me.

Upvotes: 1

Related Questions