XmlDocument doc = new XmlDocument();
doc.LoadXml("<root>"+
"<elem>some text<child/>more text</elem>" +
"</root>");比如这种情况,如果读取rootNode.innerText的话,结果是some textmore text,这里的text与more我想让他们之间有空格(为了后面的单词分析),有没有办法呢,.net似乎只提供了一个innerText方法或者,让我一个个node的innertext读也可以,我可以递归读全部加空格,这种办法也行.请给一些建议谢谢
doc.LoadXml("<root>"+
"<elem>some text<child/>more text</elem>" +
"</root>");比如这种情况,如果读取rootNode.innerText的话,结果是some textmore text,这里的text与more我想让他们之间有空格(为了后面的单词分析),有没有办法呢,.net似乎只提供了一个innerText方法或者,让我一个个node的innertext读也可以,我可以递归读全部加空格,这种办法也行.请给一些建议谢谢
1.xml<?xml version="1.0" encoding="utf-8" ?>
<root><elem>some text<child/>more text</elem></root>string s = "";
XmlReader xmlReader = XmlReader.Create(@"E:\1.xml");
while (xmlReader.Read())
{
switch (xmlReader.NodeType)
{
case XmlNodeType.Text:
s += xmlReader.Value+" ";
break;
}
}
MessageBox.Show(s.Substring(0,s.Length-1));
XmlDocument doc = new XmlDocument();
doc.LoadXml("<root>" +
"<elem>some text<child/>more text</elem>" +
"</root>");
XmlNode xn= doc.SelectNodes("//elem").Item(0);
if (xn.HasChildNodes)
{
MessageBox.Show(xn.FirstChild.InnerText); //some text
MessageBox.Show(xn.LastChild.InnerText); //more text
}
随便写的,真正用时你要先判断节点个数,避免异常
修正一下楼上的
XML是树状结构,类似档案系统内数据夹的结构,XPath也类似档案系统的路径命名方式。不过XPath 是一种模式(Pattern),可以选出 XML档案中,路径符合某个模式的所有节点出来。例如要选catalog底下的cd中所有price元素可以用: /catalog/cd/price 如果XPath的开头是一个斜线(/)代表这是绝对路径。如果开头是两个斜线(//)表示文件中所有符合模式的元素都会被选出来,即使是处于树中不同的层级也会被选出来。以下的语法会选出文件中所有叫做cd的元素(在树中的任何层级都会被选出来): //cd 选择未知的元素
使用星号(Wildcards,*)可以选择未知的元素。下面这个语法会选出/catalog/cd 的所有子元素: /catalog/cd/* 以下的语法会选出所有catalog的子元素中,包含有price作为子元素的元素。 /catalog/*/price 以下的语法会选出有两层父节点,叫做price的所有元素。 /*/*/price 以下的语法会选择出文件中的所有元素。 //* 要注意的是,想要存取不分层级的元素,XPath语法必须以两个斜线开头(//),想要存取未知元素才用星号(*),星号只能代表未知名称的元素,不能代表未知层级的元素。选择分支
使用中括号可以选择分支。以下的语法从catalog的子元素中取出第一个叫做cd的元素。XPath的定义中没有第0元素这种东西。 /catalog/cd[1] 以下语法选择catalog中的最后一个cd元素:(XPathj并没有定义 first() 这种函式喔,用上例的 [1]就可以取出第一个元素。 /catalog/cd[last()] 以下语法选出含有price子元素的所有/catalog/cd元素。 /catalog/cd[price] 以下语法选出price元素的值等于10.90的所有/catalog/cd元素 /catalog/cd[price=10.90] 以下语法选出price元素的值等于10.90的所有/catalog/cd元素 的price元素 /catalog/cd[price=10.90]/price 选择一个以上的路径
使用Or操作数(|)就可以选择一个以上的路径。例如: /catalog/cd/title | catalog/cd/artist 选择所有title以及artist元素 //title | //artist 选择所有title以及artist以及price元素 //title | //artist | //price 选择属性
在XPath中,除了选择元素以外,也可以选择属性。属性都是以@开头。例如选择文件中所有叫做country的属性: //@country 选择所有含有country这个属性的cd元素: //cd[@country] 以下语法选择出含有属性的所有cd元素 //cd[@*] 以下语法选择出country属性值为UK的cd元素 //cd[@country='UK'] 只要掌握了xpath语法,理论上你就可以访问xml文件中的任意节点和任意值
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="fantasy" ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
</bookstore>
1、往<bookstore>节点中插入一个<book>节点:
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load("bookstore.xml");
XmlNode root=xmlDoc.SelectSingleNode("bookstore");//查找<bookstore>
XmlElement xe1=xmlDoc.CreateElement("book");//创建一个<book>节点
xe1.SetAttribute("genre","李赞红");//设置该节点genre属性
xe1.SetAttribute("ISBN","2-3631-4");//设置该节点ISBN属性
XmlElement xesub1=xmlDoc.CreateElement("title");
xesub1.InnerText="CS从入门到精通";//设置文本节点
xe1.AppendChild(xesub1);//添加到<book>节点中
XmlElement xesub2=xmlDoc.CreateElement("author");
xesub2.InnerText="候捷";
xe1.AppendChild(xesub2);
XmlElement xesub3=xmlDoc.CreateElement("price");
xesub3.InnerText="58.3";
xe1.AppendChild(xesub3);
root.AppendChild(xe1);//添加到<bookstore>节点中
xmlDoc.Save("bookstore.xml");
//===============================================
结果为:
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="fantasy" ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book genre="李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</book>
</bookstore>
2、修改节点:将genre属性值为“李赞红“的节点的genre值改为“update李赞红”,将该节点的子节点<author>的文本修改为“亚胜”。
XmlNodeList nodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes;//获取bookstore节点的所有子节点
foreach(XmlNode xn in nodeList)//遍历所有子节点
{
XmlElement xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型
if(xe.GetAttribute("genre")=="李赞红")//如果genre属性值为“李赞红”
{
xe.SetAttribute("genre","update李赞红");//则修改该属性为“update李赞红”
XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点
foreach(XmlNode xn1 in nls)//遍历
{
XmlElement xe2=(XmlElement)xn1;//转换类型
if(xe2.Name=="author")//如果找到
{
xe2.InnerText="亚胜";//则修改
break;//找到退出来就可以了
}
}
break;
}
}
xmlDoc.Save("bookstore.xml");//保存。
//==================================================
最后结果为:
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="fantasy" ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book genre="update李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>亚胜</author>
<price>58.3</price>
</book>
</bookstore>
3、删除 <book genre="fantasy" ISBN="2-3631-4">节点的genre属性,删除 <book genre="update李赞红" ISBN="2-3631-4">节点。
XmlNodeList xnl=xmlDoc.SelectSingleNode("bookstore").ChildNodes;
foreach(XmlNode xn in xnl)
{
XmlElement xe=(XmlElement)xn;
if(xe.GetAttribute("genre")=="fantasy")
{
xe.RemoveAttribute("genre");//删除genre属性
}
else if(xe.GetAttribute("genre")=="update李赞红")
{
xe.RemoveAll();//删除该节点的全部内容
}
}
xmlDoc.Save("bookstore.xml");
//===========================================
最后结果为:
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book>
</book>
</bookstore>
4、显示所有数据。
XmlNode xn=xmlDoc.SelectSingleNode("bookstore");
XmlNodeList xnl=xn.ChildNodes;
foreach(XmlNode xnf in xnl)
{
XmlElement xe=(XmlElement)xnf;
Console.WriteLine(xe.GetAttribute("genre"));//显示属性值
Console.WriteLine(xe.GetAttribute("ISBN"));
XmlNodeList xnf1=xe.ChildNodes;
foreach(XmlNode xn2 in xnf1)
{
Console.WriteLine(xn2.InnerText);//显示子节点点文本
}
}
我用的是SelectNodes("//elem").Item(0);
因为考虑到他以后不一定就是只有一个节点……所以应该是没有错的,用什么依据楼主的实际需求而定