简体   繁体   English

如何阅读和编辑XML节点

[英]How to Read and Edit XML Nodes

I am fairly new to all of this and am struggling to find the answer, so any pointers in the right direction would be great. 我对所有这些都相当新,并且很难找到答案,所以任何正确方向的指针都会很棒。

I have an XML file from a system that I have no control over and it produces it like this: 我有一个我无法控制的系统的XML文件,它产生如下:

<?xml version="1.0" ?>
<WatchConfig>
<ProcessList>
    <Process>
        <RunOnDesktop>0</RunOnDesktop>
        <DaysToKeepBackup>10</DaysToKeepBackup>
        <Scheduler>
            <Active>-1</Active>
            <Startup>0</Startup>
            <SelfRepl>0</SelfRepl>
            <MaxPercent>0</MaxPercent>
            <Delay>4</Delay>
            <AsSoon>-1</AsSoon>
            <OneTime>0</OneTime>
            <Period>0</Period>
            <Week>1</Week>
            <Interval>2</Interval>
        </Scheduler>
        <UniqueName>0ZISFZL6O6S5001</UniqueName>
        <Name>Process1</Name>
        <Group/>
        <MetaFileName/>
                 </Process>
    <Process>
        <RunOnDesktop>0</RunOnDesktop>
        <DaysToKeepBackup>10</DaysToKeepBackup>
        <Scheduler>
            <Active>0</Active>
            <Startup>0</Startup>
            <SelfRepl>0</SelfRepl>
            <MaxPercent>20</MaxPercent>
            <Delay>4</Delay>
            <AsSoon>-1</AsSoon>
            <OneTime>0</OneTime>
            <Period>0</Period>
            <Week>1</Week>
            <Interval>2</Interval>
        </Scheduler>
        <UniqueName>00ZJENPXPX1KL07</UniqueName>
        <Name>Process2</Name>
        <Group/>
        <MetaFileName/>
    </Process>
</ProcessList>
</WatchConfig>

What I would like to be able to do is to read through this and amend the Active value under the Scheduler node, ideally being able to specify the name of the process that I want to amend.(as there are two in this example) 我希望能够做的是阅读这个并修改Scheduler节点下的Active值,理想情况下能够指定我想要修改的进程的名称。(在这个例子中有两个)

Easiest with XLinq: 最简单的XLinq:

var doc = XDocument.Load(fileName);
var p = doc.Descendants("Process")
      .Where(e => e.Element("UniqueName").Value == procName )
      .Single();

var a = p.Element("Scheduler").Element("Active");            
a.Value = "+1";

doc.Save(outFileName);

Maybe you can try with something like LINQ 也许你可以试试像LINQ这样的东西

U can do this to load the XML 你可以这样做来加载XML

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
public class EmployeeServices
{
    XElement _empXml = XElement.Load(@"employees.xml");
}

//And then to query the information u can do this //然后查询信息你可以做到这一点

public List<string> GetDepartments()
{
  //query the XML and group by department
   // select only the departments in the group
   var deptQuery =
   from emp in _empXml.Descendants("Employee")
   group emp by emp.Element("Department").Value
   into empGroup
   select empGroup.First().Element("Department").Value;
   return deptQuery.ToList();
}

This is an example took from the MCTS book, hope it helps. 这是MCTS书中的一个例子,希望它有所帮助。

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

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