C#:读取 XML 属性

本文关键字:属性 XML 读取 | 更新日期: 2023-09-27 18:36:33

Using C#2.0 and VIsualStudio2005

我正在尝试访问"监视器响应记录"中的"显示名称"从如下所示的 XML 文件中:

    <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

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

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

布乌特不起作用:/有什么帮助吗?

更新:

我收到的异常如下,发生在文档中。加载("...") 行:

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

C#:读取 XML 属性

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

doc.SelectSingleNode("MonitorResponseRecord/@DisplayName")

要使其在文档中的任意位置搜索,请用双斜杠作为前缀:

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

如果这仍然不起作用,我会在去除两个根节点上的所有这些命名空间声明后尝试上面的示例。

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

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

怎么样:

    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;

使用命名空间管理器,指定命名空间 URI 并在 XPath 中使用它。它对我有用。