各位好: 这里有一段代码
XElement xmlPage = XElement.Load("path"); 我想创建一个XElement,但通过上面的那句代码,必须要传一个string类型的字符串才行。传一个XPathNavigator过去,应该用什么方法?
也就是XElement xmlPage=XElement.?(XPathNavigator),或者其他方法也行,最终目的就是:通过传一个XPathNavigator来创建一个XElement.
谢谢各位!
XElement xmlPage = XElement.Load("path"); 我想创建一个XElement,但通过上面的那句代码,必须要传一个string类型的字符串才行。传一个XPathNavigator过去,应该用什么方法?
也就是XElement xmlPage=XElement.?(XPathNavigator),或者其他方法也行,最终目的就是:通过传一个XPathNavigator来创建一个XElement.
谢谢各位!
using System.Xml;
using System.Xml.XPath;namespace q308343 {
class Class1 {
static void Main(string[] args) { XPathNavigator nav;
XPathDocument docNav; docNav = new XPathDocument(@"c:\books.xml");
nav = docNav.CreateNavigator();
nav.MoveToRoot();
//Move to the first child node (comment field).
nav.MoveToFirstChild();do {
//Find the first element.
if (nav.NodeType == XPathNodeType.Element) {
//Determine whether children exist.
if (nav.HasChildren == true) {//Move to the first child.
nav.MoveToFirstChild();//Loop through all of the children.
do {
//Display the data.
Console.Write("The XML string for this child ");
Console.WriteLine("is '{0}'", nav.Value);//Check for attributes.
if (nav.HasAttributes == true) {
Console.WriteLine("This node has attributes");
}
} while (nav.MoveToNext());
}
}
} while (nav.MoveToNext());
//Pause.
Console.ReadLine();
}
}
}