我有一个带有此NODE的XML,我需要使用c#来获取该值
<my:TipoDeGasto>Gastos Regulares</my:TipoDeGasto>
我有以下代码
var myDoc = new XmlDocument();
myDoc.Load(urlFile);
XmlNamespaceManager ns = new XmlNamespaceManager(myDoc.NameTable);
ns.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2014-07-12T15:29:09");
var tipoDeGasto = myDoc.SelectSingleNode("/my:TipoDeGasto", ns);
但是我得到tipoDeGasto == null
答案 0 :(得分:0)
获取和设置信息路径表单字段值的示例代码,您可以尝试。
using (var clientContext = new ClientContext("http://sp:12001/"))
{
var web = clientContext.Web;
var myList = web.Lists.GetByTitle("Mydoc2");
var item = myList.GetItemById(6);
XmlDocument xmlItem = new XmlDocument();
//Open XML file and load it into XML document
var _File = item.File;
ClientResult<Stream> clientResult = _File.OpenBinaryStream();
clientContext.Load(_File, F => F.ServerRelativeUrl);
clientContext.ExecuteQuery();
using (var memory = new System.IO.MemoryStream())
{
byte[] buffer = new byte[1024 * 64];
int nread = 0;
while ((nread = clientResult.Value.Read(buffer, 0, buffer.Length)) > 0)
{
memory.Write(buffer, 0, nread);
}
memory.Seek(0, System.IO.SeekOrigin.Begin);
xmlItem.Load(memory);
}
XPathNavigator navItem = xmlItem.CreateNavigator();
//Redefine NameSpaceManager (Generate the NameSpace manager for this item)
navItem.MoveToFollowing(XPathNodeType.Element);
XmlNamespaceManager nsManager = new XmlNamespaceManager(new NameTable());
foreach (var ns in navItem.GetNamespacesInScope(XmlNamespaceScope.All))
{
if (ns.Key == String.Empty)
{
nsManager.AddNamespace("def", ns.Value);
}
else
{
nsManager.AddNamespace(ns.Key, ns.Value);
}
}
//Change your value here….
var node=navItem.SelectSingleNode("/my:myFields/my:field2", nsManager);
node.SetValue("NewValue");
//Save the modified xml into the item
byte[] xmlData = System.Text.Encoding.UTF8.GetBytes(xmlItem.OuterXml);
Stream NewStream = new MemoryStream(xmlData);
Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, _File.ServerRelativeUrl, NewStream, true);
//item.File.SaveBinary(xmlData);
item.Update();
clientContext.ExecuteQuery();
}