使用C#编辑XML行 [英] Editing XML lines using C#

查看:113
本文介绍了使用C#编辑XML行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还在进入整个自动化领域,还有一些我不熟悉的领域 - 今天结果是XML编辑。

我对XML文件没有多少工作,所以我正在努力解决我想做的事情。



我需要编辑XML文件中的3个版本行,增加PhoneVersion,TabletVersion和版本号的版本号。 DesktopVersion由1.



请参阅下面的XML



I’m still getting into the whole automation stuff and there are still areas that I am unfamiliar with – Today it turns out to be XML editing.
I haven’t worked much with XML files so I’m struggling slightly with what I’m trying to do.

I need to edit the 3 versions lines in the XML file, incrementing the version number of "PhoneVersion", "TabletVersion" & "DesktopVersion" by 1.

See XML below

<configuration>
  <appSettings>
    <add key="PhoneVersion" value="36.999.1" />
    <add key="TabletVersion" value="36.999.1" />
    <add key="DesktopVersion" value="36.999.1" />
    <add key="ToolsEnabledKey" value="tokolosheQA" />
    <add key="ToolsURL" value="" />
    <add key="DbCommandTimeOut" value="60" />
  </appSettings>
</configuration>





我实际上没有太多运气阅读或访问单个元素/属性。



我尝试过的事情:



这就是我试过的br $>




I haven’t had much luck actually reading or accessing the individual elements/attributes.

What I have tried:

This is what i have tried

XDocument doc = XDocument.Load(webconfig);

foreach (XElement add in doc.Descendants("add"))
 {
    string[] values = add.Attribute("value").Value.Split(new char[] { '.' });
    values[values.Length - 1] = (int.Parse(values[values.Length - 1]) + 1).ToString();
    add.SetAttributeValue("value", string.Join(".", values));
 }

推荐答案

试试这个,假设你的XML在_strXml中:



Try this, assuming your XML is in _strXml:

using System.Xml;


XmlDocument oDom = new XmlDocument();
oDom.LoadXml(_strXml);

XmlNode oXmlNode = oDom.FirstChild; // node "configurartion"
oXmlNode = oXmlNode.FirstChild;     // node "appSettings"

foreach (XmlNode oNode in oXmlNode.ChildNodes)
{
    if (oNode.Attributes["key"].Value == "PhoneVersion")
        oNode.Attributes["value"].Value = "whatever";
    // ... and so long ...
}


Xml文件不包含行! Xml [ ^ ]内容是一组节点!





试试这个:

Xml file does not contain lines! Xml[^] content is a set of nodes!


Try this:
XDocument xdoc = XDocument.Load(webconfig);
var nodes = xdoc.Descendants("add")
	.Select(x=> new
	{
		NodeName = x.Name,
		Key = (string)x.Attribute("key").Value,
		Value = (string)x.Attribute("value").Value
	})
	.ToList();





结果:



Result:

NodeName  Key               Value
add       PhoneVersion      36.999.1 
add       TabletVersion     36.999.1 
add       DesktopVersion    36.999.1 
add       ToolsEnabledKey   tokolosheQA 
add       ToolsURL   
add       DbCommandTimeOut  60 





选择单个节点:



to select single node:

XElement singlenode = xdoc.Descendants("add")
	.Where(x=> (string)x.Attribute("key").Value == "DesktopVersion")
	.SingleOrDefault();

singlenode.Attribute("value").Value = "Bla bla bla";

//save changes
xdoc.Save(webconfig)





有关操作xml内容的更多详细信息,请参阅: XDocument类(System.Xml.Linq) [ ^ ]


因为你想到了我的一些想法 - 这个工作的家伙!!



SO AFTER TINKERING WITH SOME IDEAS YOU GAVE ME - THIS WORKED GUY!!

string newValue = string.Empty;
     XmlDocument xmlDoc = new XmlDocument();

     xmlDoc.Load(webconfig);

     XmlNode PhoneVersionNode =
     xmlDoc.SelectSingleNode("configuration/appSettings/add[@key='PhoneVersion']");
     PhoneVersionNode.Attributes[1].Value = "newValue";


     xmlDoc.Save(webconfig);





感谢帮助



THANKS FOR THE HELP


这篇关于使用C#编辑XML行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆