XML属性更新

时间:2015-10-08 08:38:00

标签: .net xml powershell attributes

我有一个与此类似的XML:

<?xml version="1.0" encoding="utf-8"?>
<Configuration>
  <Format_Version>1.0</Format_Version>
  <Reporting>
  </Reporting>
  <Jobs>
        <!--<Job>
                <Name>SpaceCheck</Name>
                <Job_Type>SpaceCheck</Job_Type>
                <Schedule>
                    <Start_Time>0300</Start_Time>
                    <Frequency>24 hours</Frequency>
                    <Max_Run_Time_In_Seconds>10</Max_Run_Time_In_Seconds>
                </Schedule>
                <Parameters>
                    <Drive>C</Drive>
                    <Drive>D</Drive>
                </Parameters>
        </Job>
        <Job>
                <Name>CPUCheck</Name>
                <Job_Type>UsageMonitor</Job_Type>
                <Schedule>
                    <Frequency>3 minutes</Frequency>
                </Schedule>
                <Parameters>
                    <Threshold>90%</Threshold>
                    <Duration>10 minutes</Duration>
                </Parameters>
        </Job>
        <Job>
                <Name>overloaded CPUCheck</Name>
                <Job_Type>CPUcheck2</Job_Type>
                <Schedule>
                    <Frequency>3 minutes</Frequency>
                </Schedule>
                <Parameters>
                    <Threshold>80%</Threshold>
                    <Duration>50 minutes</Duration>
                </Parameters>
            </Job>-->
        <Job>
          <Name>Connection</Name>
          <Job_Type>Connectivity</Job_Type>
          <Schedule>
            <Start_Time>1900</Start_Time>
            <Frequency>1</Frequency>
            <Maximum_Runtime>30</Maximum_Runtime>
          </Schedule>
          <Parameters>
            <ToErrInHours>1</ToErrInHours>
            <Days>1</Days>
            <Threshold>70</Threshold>
          </Parameters>
        </Job>
      </Jobs>
    </Configuration>

我正在尝试将Start_Time的值从1900更新到2300.问题是元素更新但不是值。我该如何解决这个问题。

尝试更新 - 这样:

#Get the content of the Config File
$xdoc = [xml](Get-content "C:\Program Files (x86)\Alpha\Beta\gamma.xml")
#Get the value of the Attribute key
$StartTime = $XDoc.SelectSingleNode("//Start_Time[1]")
echo $StartTime
# Set the attribute
$StartTime.SetAttribute("Start_Time", "2300")
$xdoc.Save('C:\Program Files (x86)\Alpha\Beta\gamma.xml')
echo $StartTime

1 个答案:

答案 0 :(得分:0)

您正在尝试通过调用SetAttribute()将1900更改为2300,但是,1900不是该元素的属性。您应该设置InnerText,如下所示:

$StartTime.InnerText = "2300"

Here's来自MSDN的一个例子。它使用以下作为示例XML。请注意<title><price>与您想要更改的<Start_Time>元素的关系。

<?xml version="1.0" encoding="utf-8"?>
<books xmlns="http://www.contoso.com/books">
  <book genre="novel" ISBN="1-861001-57-8" publicationdate="1823-01-28">
    <title>Pride And Prejudice</title>
    <price>24.95</price>
  </book>
  <book genre="novel" ISBN="1-861002-30-1" publicationdate="1985-01-01">
    <title>The Handmaid's Tale</title>
    <price>29.95</price>
  </book>
  <book genre="novel" ISBN="1-861001-45-3" publicationdate="1811-01-01">
    <title>Sense and Sensibility</title>
    <price>19.95</price>
  </book>
</books>

Here是他们用来更新XML各个部分的代码。他们使用InnerText更改<title><price>的值。

public void editBook(string title, string ISBN, string publicationDate,
    string genre, string price, XmlNode book, bool validateNode, bool generateSchema)
{

    XmlElement bookElement = (XmlElement)book;

    // Get the attributes of a book.        
    bookElement.SetAttribute("ISBN", ISBN);
    bookElement.SetAttribute("genre", genre);
    bookElement.SetAttribute("publicationdate", publicationDate);

    // Get the values of child elements of a book.
    bookElement["title"].InnerText = title;
    bookElement["price"].InnerText = price;

    if (validateNode)
    {
        validateXML(generateSchema, bookElement.OwnerDocument);
    }
}