[@lm_id=3]只能是对属性的选择;
你的数据应为
<NewsSubject>
<subject lm_id=1>
<lm_name>人物</lm_name>
</subject>
</NewsSubject>
using System;
using System.IO;
using System.Xml;public class Sample
{
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); }
}[C++]
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;int main() { XmlDocument* doc = new XmlDocument();
doc -> Load(S"booksort.xml"); //Create an XmlNamespaceManager for resolving namespaces.
XmlNamespaceManager* nsmgr = new XmlNamespaceManager(doc -> NameTable);
nsmgr -> AddNamespace(S"bk", S"urn:samples"); //Select the book node with the matching attribute value.
XmlNode * book;
XmlElement * root = doc -> DocumentElement;
book = root -> SelectSingleNode(S"descendant::book->Item[@bk:ISBN='1-861001-57-6']", nsmgr); Console::WriteLine(book -> OuterXml);
}该示例使用文件 booksort.xml 作为输入。<?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>
<book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
<title>The Handmaid's Tale</title>
<author>
<first-name>Margaret</first-name>
<last-name>Atwood</last-name>
</author>
<price>29.95</price>
</book>
<book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
<title>Emma</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>19.95</price>
</book>
<book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
<title>Sense and Sensibility</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>19.95</price>
</book>
</bookstore>
你的数据应为
<NewsSubject>
<subject lm_id=1>
<lm_name>人物</lm_name>
</subject>
</NewsSubject>
using System;
using System.IO;
using System.Xml;public class Sample
{
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); }
}[C++]
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;int main() { XmlDocument* doc = new XmlDocument();
doc -> Load(S"booksort.xml"); //Create an XmlNamespaceManager for resolving namespaces.
XmlNamespaceManager* nsmgr = new XmlNamespaceManager(doc -> NameTable);
nsmgr -> AddNamespace(S"bk", S"urn:samples"); //Select the book node with the matching attribute value.
XmlNode * book;
XmlElement * root = doc -> DocumentElement;
book = root -> SelectSingleNode(S"descendant::book->Item[@bk:ISBN='1-861001-57-6']", nsmgr); Console::WriteLine(book -> OuterXml);
}该示例使用文件 booksort.xml 作为输入。<?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>
<book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
<title>The Handmaid's Tale</title>
<author>
<first-name>Margaret</first-name>
<last-name>Atwood</last-name>
</author>
<price>29.95</price>
</book>
<book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
<title>Emma</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>19.95</price>
</book>
<book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
<title>Sense and Sensibility</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>19.95</price>
</book>
</bookstore>
解决方案 »
- 未能加载文件或程序集“App_Lice
- list<T> ado
- ffmpeg 调用路径总是不对
- 200分请教日历控件mm/dd/yyy日历格式怎么改为dd/mm/yy格式,另开帖给100分
- 关于Asp.Net 网站管理工具的使用(求助)
- 请问引用的DLL多了,会影响系统性能么?
- 怎样做个漂亮的界面?
- 我动态的绑定了模板,为什么记录绑定不上去呢?
- 如何在水晶报表上画斜线,50分"倾家荡产"求助!
- DataSet新加入一行的排序问题
- 如何在CSS中指定textbox的backgroundcolor?
- 用asp.net,不学script脚本(javascript, jscritp, vbscript)行吗?
string itemname=this.listBox1.SelectedItem.ToString();
this.listBox1.Items.Remove(this.listBox1.SelectedItem);
//begin del xmlfile
XmlDocument doc=new XmlDocument();
doc.Load(xmlfile);
XmlNodeList topM=doc.DocumentElement.ChildNodes;
foreach(XmlElement element in topM)
{
if(element.Name==this.comboBox1.Text)
{
//得到该节点的子节点
XmlNodeList nodelist=element.ChildNodes;
foreach(XmlElement el in nodelist)//读元素值
{
if(el.Attributes["key"].Value==itemname)
{
element.RemoveChild(el);
}
}//循环元素
}//得到组
}//循环组
doc.Save(xmlfile); //一定要保存一下,否则不起作用
能不能根据<lm_id>1</lm_id>
选择到
<subject>
<lm_id>1</lm_id>
<lm_name>人物</lm_name>
</subject>
结点,把相应的内容全部删除??
ParentNode.RemoveChild(XmlNode)
xmldoc.DocumentElement.RemoveChild(delNode.ParentNode);-->XmlNode delNode = xmldoc.SelectSingleNode("NewsSubject/subject/[lm_id=3]");
xmldoc.DocumentElement.RemoveChild(delNode);
for(int i=0;i<xmlnd.Count;i++)
{
String lm_id = xmlnd[i].SelectSingleNode("lm_id").InnerXml.Trim();
if(lm_id=="1")
{
xmlnd[i].RemoveAll(); }
}
xmldoc.Save(xmlPath);
Response.Write("删除成功");可结果是:
<?xml version="1.0" encoding="gb2312"?>
<NewsSubject>
<subject>
</subject>
<subject>
<lm_id>2</lm_id>
<lm_name>春运</lm_name>
</subject>
<subject>
<lm_id>3</lm_id>
<l_name>栏目3</l_name>
</subject>
</NewsSubject>我想把空的
<subject>
</subject>
也删除。请问怎么做?
XmlNode delNode = xmldoc.SelectSingleNode("NewsSubject/subject/[lm_id=3]");
xmldoc.DocumentElement.RemoveChild(delNode);
还是不行啊。
报错“
传递给此方法的表达式应产生 NodeSet。
XmlNode delNode = xmldoc.SelectSingleNode("NewsSubject/subject/[lm_id=3]");
-->
XmlNode delNode = xmldoc.SelectSingleNode("NewsSubject/subject[lm_id=3]");
2002pine(我学习,我存在)来接分