C# 在wpf c中的xml中的特定位置添加元素#
我有一份wpf申请 在这里,我需要在特定的xml位置中插入一个元素标记C# 在wpf c中的xml中的特定位置添加元素#,c#,wpf,xml,C#,Wpf,Xml,我有一份wpf申请 在这里,我需要在特定的xml位置中插入一个元素标记 <Profile> <profile number = "1"> <mode>1</mode> <mode>2</mode> </profile> <profile number = "2"> <mode>1</mode
<Profile>
<profile number = "1">
<mode>1</mode>
<mode>2</mode>
</profile>
<profile number = "2">
<mode>1</mode>
<mode>2</mode>
</profile>
<profile number = "3">
<mode>1</mode>
<mode>2</mode>
</profile>
</profile>
1.
2.
1.
2.
1.
2.
在这里,我想在第一个配置文件标记内添加模式标记,即。
如何在profile标记中找到number标记并使用c#向其插入子节点(如)。
1.
2.
3.
请帮忙!!
使用以下方法:
String xmlString=”“+“”+
""+
" "+
" 1"+ //1
" 2"+ //2
“3”+//3,我需要插入它
“4”+//4,我需要第二次插入
" 5"+
" 6"+
" "+
" "+
"";
XElement root=XElement.Parse(xmlString);
var childrens=root.substands(“children”).ToArray();
var third=儿童[3];
var第四=儿童[4];
第三,在自己之前添加(新元素(“儿童”);
第四,在自己之前添加(新元素(“儿童”);
var updatedchildren=root.substands(“children”).ToArray();
您可以使用XPATH选择所需的元素并向其中添加子元素
string yourxml = "<Profile><profile number = \"1\"><mode>1</mode><mode>2</mode></profile><profile number = \"2\"><mode>1</mode><mode>2</mode></profile><profile number = \"3\"><mode>1</mode><mode>2</mode></profile></Profile>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(yourxml);
//Selecting node with number='3'
XmlNode profile;
XmlElement root = doc.DocumentElement;
profile = root.SelectSingleNode("profile[@number = '3']");
XmlElement newChild = doc.CreateElement("mode");
newChild.InnerText = "1";
profile.AppendChild(newChild);
doc.Save("file path");
string yourxml=“121212”;
XmlDocument doc=新的XmlDocument();
LoadXml(yourxml);
//正在选择编号为='3'的节点
XmlNode配置文件;
XmlElement根=doc.DocumentElement;
profile=root.SelectSingleNode(“profile[@number='3']”);
XmlElement newChild=doc.CreateElement(“模式”);
newChild.InnerText=“1”;
附录儿童(新儿童);
保存文件(“文件路径”);
<profile number = "1">
<mode> 1 </mode>
<mode> 2 </mode>
<mode> 3 </mode>
</profile>
String xmlString = "<?xml version=\"1.0\"?>"+"<xmlhere>"+
"<somenode>"+
" <child> </child>"+
" <children>1</children>"+ //1
" <children>2</children>"+ //2
" <children>3</children>"+ // 3, I need to insert it
" <children>4</children>"+ //4, I need to insert this second time
" <children>5</children>"+
" <children>6</children>"+
" <child> </child>"+
" </somenode>"+
"</xmlhere>";
XElement root = XElement.Parse(xmlString);
var childrens = root.Descendants("children").ToArray();
var third = childrens[3];
var fourth = childrens[4];
third.AddBeforeSelf(new XElement("children"));
fourth.AddBeforeSelf(new XElement("children"));
var updatedchildren = root.Descendants("children").ToArray();
string yourxml = "<Profile><profile number = \"1\"><mode>1</mode><mode>2</mode></profile><profile number = \"2\"><mode>1</mode><mode>2</mode></profile><profile number = \"3\"><mode>1</mode><mode>2</mode></profile></Profile>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(yourxml);
//Selecting node with number='3'
XmlNode profile;
XmlElement root = doc.DocumentElement;
profile = root.SelectSingleNode("profile[@number = '3']");
XmlElement newChild = doc.CreateElement("mode");
newChild.InnerText = "1";
profile.AppendChild(newChild);
doc.Save("file path");