如何使用c#

时间:2017-01-09 06:20:08

标签: c# xml attributes

我必须将'Hierarchy_RestrictedOperations'的xml属性值更改为TRUE为FALSE。 这里,xml看起来像

    <BusinessRules>
    <BusinessRule>
        <Type>All_NoEmptyRequiredProperty</Type>
        <Enabled ptype="BOOLEAN">TRUE</Enabled>
    </BusinessRule>
    <BusinessRule>
        <Type>All_CheckStringLength</Type>
        <Enabled ptype="BOOLEAN">TRUE</Enabled>
    </BusinessRule>
    <BusinessRule>
<BusinessRule>
    <Type>Hierarchy_RestrictedOperations</Type>
    <Enabled ptype="BOOLEAN">TRUE</Enabled>
</BusinessRule> 
<BusinessRule>
  <Type>ProdOff_AllowAccountPOCurrencyMismatch</Type>
  <Enabled ptype="BOOLEAN">FALSE</Enabled>
</BusinessRule>
<!-- Following business rule was added for FEAT-147 -->
<BusinessRule>
  <Type>ProdOff_AllowMultiplePISubscriptionRCNRC</Type>
  <Enabled ptype="BOOLEAN">FALSE</Enabled>
</BusinessRule>
<!-- Following business rule was added for CORE-10776 -->
<BusinessRule>
  <Type>ImmediateSubscriptionTermination</Type>
  <Enabled ptype="BOOLEAN">FALSE</Enabled>
</BusinessRule>

任何人都可以帮我使用c#我试过下面的代码

     XmlDocument xml = new XmlDocument();
  xml.Load("R:\\config\\ProductCatalog\\PCConfig.xml");


  XmlNodeList nodes = xml.SelectNodes("//BusinessRule");
  //XmlNodeList type = xml.SelectNodes("//Hierarchy_RestrictedOperations");
  foreach (XmlElement element in nodes)
    {

      element.SelectSingleNode("Type").InnerText = "Hierarchy_RestrictedOperations";

    }
    xml.Save("R:\\config\\ProductCatalog\\PCConfig.xml");

2 个答案:

答案 0 :(得分:0)

你可以这样做

XDocument xdc = XDocument.Load(YourXMLFile);
xdc.Descendants("BusinessRule")
   .LastOrDefault()
   .Descendants("Enabled")
   .FirstOrDefault()
   .Value = "False";
xdc.Save(YourXMLFile);

XDocument xdc = XDocument.Load(YourXMLFile);    
xdc.Descendants("BusinessRule")
   .Where(x => x.Descendants("Type")
                .FirstOrDefault()
                .Value == "Hierarchy_RestrictedOperations"
          )
   .Descendants("Enabled")
   .FirstOrDefault()
   .Value = "False";

答案 1 :(得分:0)

试试这个:

XElement element = xml.Root.Elements("BusinessRule").Where(xElement => xElement.Element("Type").Value == "Hierarchy_RestrictedOperations").FirstOrDefault();

        if(element != null)
        {
            element.Element("Enabled").Value = "FALSE";
        }

        xml.Save("R:\\config\\ProductCatalog\\PCConfig.xml");

确保xml文件的标记都正确无误。 希望这很有用。