C#SelectSingleNode-可以递归使用吗?
如中所示,如果我有一个XML文档C#SelectSingleNode-可以递归使用吗?,c#,xml,selectsinglenode,C#,Xml,Selectsinglenode,如中所示,如果我有一个XML文档 <root a="value"> <item name="first"> x <foo name = "firstgrandchild">There is nothing here</foo> y <foo name = "secondgrandchild">There is something here</foo>
<root a="value">
<item name="first">
x
<foo name = "firstgrandchild">There is nothing here</foo>
y
<foo name = "secondgrandchild">There is something here</foo>
</item>
<item name="second">
xy
<foo/>
ab
</item>
</root>
虽然属性的第一次搜索和更新工作正常,但第二次搜索和更新失败,因为mySearchNode.SelectSingleNode返回null
问题-此代码是否存在根本问题?
为什么SelectSingleNode在第二个实例中不能按预期工作,就它而言,我正在元素类型的XmlNode上执行它
请帮忙
非常感谢,mySearchNode是
item
节点,因此如果foo
是item
的子节点,那么第二个xpath应该是“foo”
您的第二个xpath查询应该没有前导斜杠指“文件根目录”。如果省略斜杠,查询将与mySearchNode变量相关。您也不应该再次包含“item”,因为您的查询是相对于您选择的“item”节点的。代码:
myDoc.Load("Items2.xml");
myNode = myDoc.DocumentElement;
mySearchNode = myNode.SelectSingleNode("/root/item");
mySearchNode.Attributes["name"].Value = "Joel";
Console.WriteLine(mySearchNode.OuterXml);
mySearchChildNode = mySearchNode.SelectSingleNode("foo");
Console.WriteLine(mySearchChildNode.OuterXml);
您可以执行SelectNodes操作,然后循环遍历所有项目节点。对于每个项目,您应该进一步在foo节点上选择nodes。您应该检查节点计数以及item和foo节点的属性名称是否存在。 您可以使用以下代码:
/// <summary>
/// Changes the xml in sourceFileName and writes the changed xml to destFileName
/// </summary>
public static void ProcessNames(string sourceFileName, string destFileName)
{
XmlDocument xmlDoc = new XmlDocument();
XmlTextWriter xtw = null;
xmlDoc.Load(sourceFileName);
try
{
// Parse through all the item nodes and process them
XmlNodeList itemList = xmlDoc.SelectNodes("//root/item");
if (itemList.Count > 0)
{
foreach (XmlNode item in itemList)
{
// Change the name attribute, if it exists
if (item.Attributes["name"] != null)
{
string newItemName = item.Attributes["name"].Value + "Joel";
item.Attributes["name"].Value = newItemName;
}
// Parse through all the foo nodes and process them
XmlNodeList fooList = item.SelectNodes("foo");
if (fooList.Count > 0)
{
foreach (XmlNode foo in fooList)
{
// Change the name attribute, if it exists
if (foo.Attributes["name"] != null)
{
string newFooName = foo.Attributes["name"].Value + "Joel";
foo.Attributes["name"].Value = newFooName;
}
}
}
}
xtw = new XmlTextWriter(destFileName, Encoding.UTF8);
xmlDoc.WriteContentTo(xtw);
}
}
finally
{
xtw.Close();
}
}
//
///更改sourceFileName中的xml,并将更改后的xml写入destFileName
///
公共静态void进程名(string sourceFileName、string destFileName)
{
XmlDocument xmlDoc=新的XmlDocument();
XmlTextWriter xtw=null;
Load(sourceFileName);
尝试
{
//解析所有项目节点并对其进行处理
XmlNodeList itemList=xmlDoc.SelectNodes(//root/item);
如果(itemList.Count>0)
{
foreach(itemList中的XmlNode项)
{
//更改名称属性(如果存在)
if(item.Attributes[“name”!=null)
{
字符串newItemName=item.Attributes[“name”].Value+“Joel”;
item.Attributes[“name”]。Value=newItemName;
}
//解析并处理所有的foo节点
XmlNodeList-doulist=item.SelectNodes(“foo”);
如果(傻瓜计数>0)
{
foreach(傻瓜列表中的xmlnodefoo)
{
//更改名称属性(如果存在)
if(foo.Attributes[“name”!=null)
{
字符串newFooName=foo.Attributes[“name”].Value+“Joel”;
foo.Attributes[“name”]。Value=newFooName;
}
}
}
}
xtw=新的XmlTextWriter(destFileName,Encoding.UTF8);
xmlDoc.writeContento(xtw);
}
}
最后
{
xtw.Close();
}
}
/// <summary>
/// Changes the xml in sourceFileName and writes the changed xml to destFileName
/// </summary>
public static void ProcessNames(string sourceFileName, string destFileName)
{
XmlDocument xmlDoc = new XmlDocument();
XmlTextWriter xtw = null;
xmlDoc.Load(sourceFileName);
try
{
// Parse through all the item nodes and process them
XmlNodeList itemList = xmlDoc.SelectNodes("//root/item");
if (itemList.Count > 0)
{
foreach (XmlNode item in itemList)
{
// Change the name attribute, if it exists
if (item.Attributes["name"] != null)
{
string newItemName = item.Attributes["name"].Value + "Joel";
item.Attributes["name"].Value = newItemName;
}
// Parse through all the foo nodes and process them
XmlNodeList fooList = item.SelectNodes("foo");
if (fooList.Count > 0)
{
foreach (XmlNode foo in fooList)
{
// Change the name attribute, if it exists
if (foo.Attributes["name"] != null)
{
string newFooName = foo.Attributes["name"].Value + "Joel";
foo.Attributes["name"].Value = newFooName;
}
}
}
}
xtw = new XmlTextWriter(destFileName, Encoding.UTF8);
xmlDoc.WriteContentTo(xtw);
}
}
finally
{
xtw.Close();
}
}