1

我对这一切都很陌生,并且正在努力寻找答案,所以任何指向正确方向的指针都会很棒。

我有一个来自我无法控制的系统的 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>

我想要做的是通读它并修改调度程序节点下的活动值,理想情况下能够指定我要修改的进程的名称。(因为在这个例子中有两个)

4

2 回答 2

3

使用 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);
于 2012-10-03T18:06:27.967 回答
1

也许您可以尝试使用 LINQ 之类的东西

你可以这样做来加载 XML

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

//然后查询信息你可以这样做

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();
}

这是从 MCTS 书中摘录的示例,希望对您有所帮助。

于 2012-10-03T18:07:08.370 回答