110,579
社区成员
发帖
与我相关
我的任务
分享
<?xml version="1.0" encoding="utf-8" ?>
<Table>
<node ID="1" PintName="abc" objectId="87678" FunctionId="1"/>
<node ID="2" PintName="def" objectId="39392" FunctionId="1"/>
<node ID="3" PintName="ghi" objectId="996378" FunctionId="2"/>
<node ID="4" PintName="jkl" objectId="43232" FunctionId="2"/>
<node ID="5" PintName="mnl" objectId="321323" FunctionId="2"/>
</Table>
string xmlPath = Application.StartupPath;
xmlPath = xmlPath.Replace("bin\\Debug", "XMLFile1.xml");
XDocument xdoc = XDocument.Load(xmlPath);
var query = from x in xdoc.Descendants("node")
where x.Attribute("FunctionId").Value == "2"
select x.Attribute("PintName").Value;
foreach (var q in query)
{
_PointName2.Add(q);
}
// 增加node节点
xdoc.Descendants("Table").FirstOrDefault().Add(
new XElement("node",
new XAttribute("ID", "1"),
new XAttribute("PintName", "ttt"),
new XAttribute("objectId", "3333333"),
new XAttribute("FunctionId", "3")));
xdoc.Save(xmlPath);
// 删除Id=3的node节点
var node1 = xdoc.Descendants("node").Where(x => x.Attribute("ID").Value == "3").FirstOrDefault();
if (node1 != null)
node1.Remove();
xdoc.Save(xmlPath);
// 修改Id=2的node节点
var node2 = xdoc.Descendants("node").Where(x => x.Attribute("ID").Value == "2").FirstOrDefault();
if (node2 != null)
node2.Attribute("PintName").Value = "abc";
xdoc.Save(xmlPath);
XDocument inventoryDoc =
new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XComment("Current Inventory of AutoLot"),
new XElement("Inventory",
new XElement("Car", new XAttribute("ID", "1"),
new XElement("Color", "Green"),
new XElement("Make", "BMW"),
new XElement("PetName", "Stan")
),
new XElement("Car", new XAttribute("ID", "2"),
new XElement("Color", "Pink"),
new XElement("Make", "Yugo"),
new XElement("PetName", "Melvin")
)
)
);
// Display the document and save to disk.
Console.WriteLine(inventoryDoc);
inventoryDoc.Save("SimpleInventory.xml");
}