C#:读取XML属性

Med*_*000 4 c# xml visual-studio-2005 c#-2.0

使用C#2.0和VIsualStudio2005

我正在尝试从一个XML文件中访问"MonitorResponseRecord"中的"DisplayName",如下所示:

    <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
Run Code Online (Sandbox Code Playgroud)

我目前的C#尝试如下,但是当我尝试时它总是挂起 XmlDocument.Load("");

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

BUUUUT不会工作:/任何帮助吗?

更新:

我收到的例外情况如下,并在doc.Load("...")行发生:

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

Kri*_*isG 5

您的XPath查询将相对于文档根目录:

doc.SelectSingleNode("MonitorResponseRecord/@DisplayName")
Run Code Online (Sandbox Code Playgroud)

要使其在doc前缀中的任何位置搜索,请使用双斜杠:

doc.SelectSingleNode("//MonitorResponseRecord/@DisplayName")
Run Code Online (Sandbox Code Playgroud)

如果仍然无法工作,我会在两个根节点上删除所有这些命名空间声明后尝试上面的例子.

否则,使用命名空间声明,您可能会发现需要定义XML命名空间映射并在XPath中使用前缀,如:

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

doc.SelectSingleNode("//x:MonitorResponseRecord/@DisplayName")
Run Code Online (Sandbox Code Playgroud)