c# - 使用 XmlNode.selectSingleNode 选择一个节点

标签 c# .net xml xpath

我使用以下代码将项目添加到 SP 列表,使用网络服务:

XmlNode returnValue = lists.UpdateListItems("Facturas", batchElement);
XmlNodeList errors = returnValue.SelectNodes("/Results");
if (errors.Count != 1)
{
   Console.WriteLine("errors.Count es " + errors.Count);
   Console.ReadKey();
   return -1;
}
Console.WriteLine("Error " + errors[0].Value + " -> " + int.Parse(errors[0].Value));

errors.OuterXml返回如下XML(z:row的属性已省略)

<Results xmlns="http://schemas.microsoft.com/sharepoint/soap/">
  <Result ID="1,New" xmlns="http://schemas.microsoft.com/sharepoint/soap/">
    <ErrorCode>0x00000000</ErrorCode>
    <ID />
    <z:row ows_ContentTypeId="0x010031045FE2D0730F499569DE68AFDB3F0B" ... xmlns:z="#RowsetSchema" />
  </Result>
</Results>

当我运行代码时,我总是得到 errors.Count 为 0。我尝试了 SelectNodes 方法的以下参数:

ErrorCode
//ErrorCode
/Results
Results
*[local-name() = 'ErrorCode']
/*[local-name() = 'Results']

此外,我将代码更改为:

XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("soap", "http://schemas.microsoft.com/sharepoint/soap/");
nsmgr.AddNamespace("rs", "urn:schemas-microsoft-com:rowset");
nsmgr.AddNamespace("z", "#RowsetSchema");
XmlNode returnValue = lists.UpdateListItems("Facturas", batchElement);
XmlNodeList errors = returnValue.SelectNodes("soap:ErrorCode", nsmgr);

并且在查询 soap:ErrorCoders:ErrorCode 时没有得到任何结果。

最佳答案

var doc = new XmlDocument();
doc.Load("1.xml");


var nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("soap", "http://schemas.microsoft.com/sharepoint/soap/");

var results = doc.SelectSingleNode("/soap:Results", nsmgr);
var errorcode = doc.SelectSingleNode("/soap:Results/soap:Result/soap:ErrorCode", nsmgr);

Console.WriteLine(errorcode.InnerText);

示例 XML:

<Results xmlns="http://schemas.microsoft.com/sharepoint/soap/">
  <Result ID="1,New" xmlns="http://schemas.microsoft.com/sharepoint/soap/">
    <ErrorCode>0x00000000</ErrorCode>
    <ID />
    <z:row ows_ContentTypeId="0x010031045FE2D0730F499569DE68AFDB3F0B" xmlns:z="#RowsetSchema" />
  </Result>
</Results>

关于c# - 使用 XmlNode.selectSingleNode 选择一个节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7431803/

相关文章:

c# - BeginInvoke 未在 WPF 应用程序的线程内触发

c# - 输入的字符串格式不正确

C#/.NET Timers 和 Win32 Sleep 函数都不准确

c# - 当我 Assembly.Load 肯定存在的程序集时,为什么会出现 FileNotFoundException?

xml - 仅使用 XPath 在 VTD-XML 中进行动态查找

c# - 在 Controller 外部将HttpResponseMessage转换为HttpActionResult的最简单方法

c# - 将 Excel 范围转换为 ADO.NET 数据集或数据表等

c# - C# 在评估/解析表达式时是否有影响?

php - 有条件地在 magento 布局中添加 block

xml - Kotlin 上 JacksonXmlElementWrapper 的问题