[英]How to change attribute type <TRUE/FALSE> in xml using c#
I have to change xml attribute value for 'Hierarchy_RestrictedOperations' toTRUE to FALSE. 我必须将“ Hierarchy_RestrictedOperations”的xml属性值更改为TRUE到FALSE。 Here, the xml looks like 在这里,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>
Can anybody help me using c# I tried with below code 任何人都可以使用下面的代码尝试使用的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");
You can do it something like this 你可以这样做
XDocument xdc = XDocument.Load(YourXMLFile);
xdc.Descendants("BusinessRule")
.LastOrDefault()
.Descendants("Enabled")
.FirstOrDefault()
.Value = "False";
xdc.Save(YourXMLFile);
or 要么
XDocument xdc = XDocument.Load(YourXMLFile);
xdc.Descendants("BusinessRule")
.Where(x => x.Descendants("Type")
.FirstOrDefault()
.Value == "Hierarchy_RestrictedOperations"
)
.Descendants("Enabled")
.FirstOrDefault()
.Value = "False";
Try this: 尝试这个:
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");
Make sure that the tags of the xml file are all correct. 确保xml文件的标签全部正确。 Hope this was useful. 希望这是有用的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.