迭代XML文档 [英] Iterate through an XML document

查看:63
本文介绍了迭代XML文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML文档,想要迭代直到我找到一个特定元素并获取特定属性中的值。



XML是一个片段关注



I have an XML document and want to iterate through until I find a particular element and get a value in a particular attribute.

The XML is a snippet as follows

<?xml version="1.0" encoding="utf-8"?>
<Eib:Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="" modificationDate="2015-08-14T17:15:11.647" version="1702.02.00BUILD202_DISCO" remoteShutdownAllowed="true" ModelBuilderVersion="6.3" E39Compatible="false" xmlns:Eib="urn:peergroup-com:Eib">
  <Eib:Log name="LogManager">
    <Eib:DatedFileLog rolloverOnStartup="true" rolloverOnMidnight="true" rolloverOnSize="5000" rolloverAgeLimit="0" rolloverFileCountLimit="0" logFilePath="C:\Temp\Logs" loggingLevel="4" enabled="true" />
  </Eib:Log>
  <Eib:Units enabled="true">
    <Eib:Unit name=" per min" symbol="/min" enabled="true" />
    <Eib:Unit name="10x6deg" symbol="10^-6deg" enabled="true" />
    <Eib:Unit name="10x_3deg" symbol="10^-3deg" enabled="true" />
    <Eib:Unit name="bps" symbol="bps" enabled="true" />
    <Eib:Unit name="chips" symbol="chips" enabled="true" />
    <Eib:Unit name="kilobel" symbol="kB" enabled="true" />
    <Eib:Unit name="lines" symbol="lines" enabled="true" />
    <Eib:Unit name="mJ per cm2" symbol="mJ/cm2" enabled="true" />
    <Eib:Unit name="nanometer per second (plane)" symbol="nm/sec" enabled="true" />
    <Eib:Unit name="Pixel" symbol="Pixel" enabled="true" />
    <Eib:Unit name="Times" symbol="Times" enabled="true" />
    <Eib:Unit name="works" symbol="works" enabled="true" />
  </Eib:Units>
  <Eib:Clock name="Clock" clockSetMethod="useOffset" />
  <Eib:ExceptionManager name="ExceptionManager" />
  <EibModel:EquipmentElements>
        <EibModel:Module name="GenericService" elementType="GenericService" function="GenericService" immutableID="GenericService" make="GenericService" model="GenericService" modelRevision="GenericService" supplier="GenericService" pollingInterval="0" processName="GenericService" processType="Process" recipeType="GenericService" enabled="true">
            <EibModel:CustomAttributes enabled="true">
              <EibModel:CustomAttribute name="ACCUM_UNIT_PROCESSED_RUNTIME_COUNTER" parameterType="Double" group="GenericService" value="0" classification="Configuration" accessLevel="0" persistValue="false" enabled="true" />
			  <EibModel:CustomAttribute name="TEMP_PATH" parameterType="StringData" group="GenericService" value="C:\Temp\DS-DISCO-DFD6361-017" classification="Configuration" accessLevel="0" persistValue="false" enabled="true" />
              <EibModel:CustomAttribute name="THRESHOLD" parameterType="IntegerCount" group="GenericService" value="5" classification="Configuration" accessLevel="0" persistValue="false" enabled="true" />
            </EibModel:CustomAttributes >
		  </EibModel:Module>    
		<EibModel:IODevice name="EquipmentConstantsContainer" uid="7eec9303-6f9f-4c10-ab78-8cd5435730ff" elementType="EquipmentConstantsContainer" function="EquipmentConstantsContainer" immutableID="EquipmentConstantsContainer" make="EquipmentConstantsContainer" model="EquipmentConstantsContainer" modelRevision="EquipmentConstantsContainer" supplier="EquipmentConstantsContainer" pollingInterval="0" enabled="true">
            <EibModel:CustomAttributes enabled="true">
              <EibModel:CustomAttribute name="AB_MODE" parameterType="StringData" group="EquipmentConstants" classification="Configuration" description="Min: NORMAL, Max: SPECIAL" accessLevel="0" persistValue="false" enabled="true" />
			</EibModel:CustomAttributes>
		</EibModel:IODevice>
	</EibModel:EquipmentElements>





我想在一个元素中得到1个特定值,让我说我想要TEMP_PATH值



< b>我尝试了什么:





I want to get 1 particular value in an element lets say I want TEMP_PATH value

What I have tried:

if (!isFound)
            {
                if (currentNode.Name != "EibModel:CustomAttribute")
                {
                    //check if node has children
                    if (currentNode.HasChildNodes)
                    {
                        XmlNodeList nodelist = currentNode.ChildNodes;

                        foreach (XmlNode node in nodelist)
                        {
                            checkNode(eibPath, node);
                        }
                    }
                    else //has no children
                    {
                        if (currentNode.NextSibling != null)
                        {
                            checkNode(eibPath, currentNode.NextSibling);
                        }
                    }
                }
                else if (currentNode.Name == "EibModel:CustomAttribute")
                {
                    if (currentNode.OuterXml.Contains("EQP_NAME"))
                    {
                        eqpName = currentNode.Attributes.GetNamedItem("value").Value;
                        tempPath = string.Concat(tempPath + eqpName);
                    }
                    if (currentNode.OuterXml.Contains("TEMP_PATH"))
                    {
                        isFound = true;
                        if (txtResults.Text != string.Empty)
                            txtResults.AppendText(Environment.NewLine);

                        txtResults.AppendText("Attempting to Process File : " + eibPath + Environment.NewLine);
                        string val = currentNode.Attributes.GetNamedItem("value").Value;

                        //create directory before changing EIB file
                        log.Info("Attempting to create folder: " + tempPath);
                        // Determine whether the directory exists.
                        if (Directory.Exists(tempPath))
                        {
                            txtResults.SelectionColor = Color.OrangeRed;
                            txtResults.AppendText(tempPath + " already exists." + Environment.NewLine);
                        }
                        else
                        {
                            // Try to create the directory.
                            DirectoryInfo di = Directory.CreateDirectory(tempPath);
                            txtResults.SelectionColor = Color.Green;
                            txtResults.AppendText(tempPath + " created succesfully." + Environment.NewLine);
                        }
                        currentNode.Attributes.GetNamedItem("value").Value = val.Replace(val, tempPath);
                        txtResults.AppendText("Old TEMP_PATH= " + val + " New TEMP_PATH= " + tempPath + Environment.NewLine);
                        //return isFound;                    
                    }
                }
                else { };                   
            }
            return isFound;
        }

推荐答案

I would build a supporting model in code, load and parse the entire xml file, and then use LINQ on the model to find what I’m after.
I would build a supporting model in code, load and parse the entire xml file, and then use LINQ on the model to find what I'm after.


这篇关于迭代XML文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆