<?xml version="1.0" encoding="utf-8" ?>
<product>
<description>Updating</description>
<filelist count="1">
<item name="ASMIS.UI.exe" size="">
<value />
</item>
<item name="test2.dll" size="">
<value />
</item>
</filelist>
</product>
请问如何修改count的值,我想把修改成任意的值,如:count="5",谢谢!
<product>
<description>Updating</description>
<filelist count="1">
<item name="ASMIS.UI.exe" size="">
<value />
</item>
<item name="test2.dll" size="">
<value />
</item>
</filelist>
</product>
请问如何修改count的值,我想把修改成任意的值,如:count="5",谢谢!
<product>
<filelist count="2">
<item name="ASMIS.UI.exe">
</item>
<item name="test2.dll">
</item>
</filelist>
</product>
再问一个问题,如何添加节点,如:
<?xml version="1.0" encoding="utf-8" ?>
<product>
<filelist count="2">
<item name="ASMIS.UI.exe">
</item>
<item name="test2.dll">
</item>
</filelist>
</product>
谢谢!
<product>
<filelist count="1">
<item name="ASMIS.UI.exe">
</item>
</filelist>
</product>
再问一个问题,如何添加节点,如:
<?xml version="1.0" encoding="utf-8" ?>
<product>
<filelist count="2">
<item name="ASMIS.UI.exe">
</item>
<item name="test2.dll">
</item>
</filelist>
</product>
谢谢!
参考一下这个
{
public static void Main()
{ XmlDocument doc = new XmlDocument();
doc.Load("booksort.xml"); //Create an XmlNamespaceManager for resolving namespaces.
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("bk", "urn:samples"); //Select the book node with the matching attribute value.
XmlNode book;
XmlElement root = doc.DocumentElement;
book = root.SelectSingleNode("descendant::book[@bk:ISBN='1-861001-57-6']", nsmgr); Console.WriteLine(book.OuterXml); }
}
<?xml version="1.0"?>
<!-- A fragment of a book store inventory database -->
<bookstore xmlns:bk="urn:samples">
<book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-8">
<title>Pride And Prejudice</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>24.95</price>
</book>
</bookstore>
<?xml version="1.0" encoding="utf-8" ?>
<product>
<description>Updating</description>
<filelist count="1">
<item name="ASMIS.UI.exe" size="">
<value />
</item>
</filelist>
</product>c#:
修改节点属性值:
XmlDocument doc = new XmlDocument();
doc.Load("XMLFile9.xml"); XmlNode rootnode = doc.SelectSingleNode("//product/filelist");
if ( rootnode != null )
{
XmlAttribute xa = rootnode.Attributes["count"];
xa.InnerText = "2";
} doc.Save("XMLFile9.xml");//添加节点
XmlDocument doc = new XmlDocument();
doc.Load("XMLFile9.XML");
XmlNode rootnode = doc.SelectSingleNode("//product");
XmlElement item2 = doc.CreateElement("item");
rootnode.AppendChild(item2); XmlAttribute xa = doc.CreateAttribute("name");
xa.InnerText="test2.dll";
item2.Attributes.Append(xa);
doc.Save("XMLFile9.xml");程序运行后的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<product>
<description>Updating</description>
<filelist count="2">
<item name="ASMIS.UI.exe" size="">
<value />
</item>
</filelist>
<item name="test2.dll" />
</product>