简体   繁体   English

C#:读取XML属性

[英]C#: Read XML Attribute

Using C#2.0 and VIsualStudio2005 使用C#2.0和VIsualStudio2005

I'm trying to access the "DisplayName" inside "MonitorResponseRecord" from an XML file like the one Below: 我正在尝试从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

My current C# attempt is as follows, BUT it always hangs up when I try to XmlDocument.Load(""); 我当前的C#尝试如下,但是当我尝试XmlDocument.Load("");时,它总是挂断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? BUUUUT无法正常工作://有任何帮助吗?

UPDATE: 更新:

the exception I recieve is as follows, and occures at the doc.Load("...") line: 我收到的异常如下,发生在doc.Load(“ ...”)行:

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

Your XPath query will be relative to the document root: 您的XPath查询将相对于文档根目录:

doc.SelectSingleNode("MonitorResponseRecord/@DisplayName")

To make it search anywhere in the doc prefix it with double slash: 要使其在doc前缀中带有双斜杠的任何位置进行搜索:

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: 否则,使用命名空间声明,您可能会发现需要定义XML命名空间映射并在XPath中使用前缀,例如:

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

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

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. 使用名称空间管理器,指定名称空间URI并在XPath中使用它。 It works for me. 这个对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM