XmlDocument.SelectNodes不返回InfoPath XML的节点
本文关键字:XML 节点 InfoPath 返回 SelectNodes XmlDocument | 更新日期: 2023-09-27 18:04:49
我试图查询InfoPath XML文档的附件,但查询未能返回任何节点。我没有太多使用XmlDocument类的经验。我认为我的问题出在命名空间上。有人对此有什么见解吗?
// Get the binary stream of the file
Stream formStream = file.OpenBinaryStream();
// Load the stream into an XPathDocument object
XmlDocument ipForm = new XmlDocument();
ipForm.Load(formStream);
//Create a NameSpaceManager object and add the namespace of the form to it
XmlNamespaceManager ns = new XmlNamespaceManager(ipForm.NameTable);
ns.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2015-07-13T20:54:32"); //http://schemas.microsoft.com/office/infopath/2003/myXSD/2015-07-13T20:54:32 | http://www.w3.org/XML/1998/namespace
//Get the XML node containing the file attachment
//XmlNode attachmentNode = ipForm.SelectSingleNode("/my:myFields/my:Attachments", ns);
//No nodes returned here
XmlNodeList myNodeList = ipForm.SelectNodes("/my:myFields/my:Attachments", ns);
XML> <?xml version="1.0" encoding="UTF-8"?><?mso-infoPathSolution solutionVersion="1.0.0.830" productVersion="14.0.0" PIVersion="1.0.0.0" href="http://sharepoint.calpine.com/sites/dept/fleetsupport/issues/Issues/Forms/template.xsn" name="urn:schemas-microsoft-com:office:infopath:Issues:-myXSD-2015-03-30T21-16-50" ?><?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.3"?><?mso-infoPath-file-attachment-present?><my:myFields xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:pc="http://schemas.microsoft.com/office/infopath/2007/PartnerControls" xmlns:ma="http://schemas.microsoft.com/office/2009/metadata/properties/metaAttributes" xmlns:d="http://schemas.microsoft.com/office/infopath/2009/WSSList/dataFields" xmlns:q="http://schemas.microsoft.com/office/infopath/2009/WSSList/queryFields" xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:dms="http://schemas.microsoft.com/office/2009/documentManagement/types" xmlns:tns="http://schemas.microsoft.com/sharepoint/soap/" xmlns:s1="http://microsoft.com/wsdl/types/" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:ns1="http://microsoft.com/webservices/SharePointPortalServer/UserProfileService" xmlns:ns2="http://schemas.microsoft.com/sharepoint/soap/directory/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:ns3="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2015-03-30T21:16:50" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xml:lang="en-us">
<my:InstrumentTag>222</my:InstrumentTag>
<my:IssueDate>2015-07-25</my:IssueDate>
<my:listName>{B6CDAA90-0F94-4127-A50F-D599952B23CD}</my:listName>
<my:strFormName>TestFormName</my:strFormName>
<my:Attachments>
<my:Attachment>/*AttachmentBinary*/</my:Attachment>
</my:Attachments>
<my:Attachments>
<my:Attachment>/*AttachmentBinary*/</my:Attachment>
</my:Attachments>
试试这个
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication37
{
class Program
{
const string FILENAME = @"'temp'test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
var results = doc.Descendants().Where(x => x.Name.LocalName == "myFields").Select(y => new
{
instrumentTag = y.Elements().Where(z => z.Name.LocalName == "InstrumentTag").FirstOrDefault().Value,
issueDate = y.Elements().Where(z => z.Name.LocalName == "IssueDate").FirstOrDefault().Value,
listName = y.Elements().Where(z => z.Name.LocalName == "listName").FirstOrDefault().Value,
strFormName = y.Elements().Where(z => z.Name.LocalName == "strFormName").FirstOrDefault().Value,
Attachments = y.Elements().Where(z => z.Name.LocalName == "Attachments").Select(a => new {
attachment = a.Elements().Where(b => b.Name.LocalName == "Attachment").Select(c => c.Value).FirstOrDefault()
}).ToList(),
}).ToList();
}
}
}
如果您确定名称空间在您的情况下没有任何意义,您可以通过在XPath中使用local-name()
来忽略它们,例如:
var xpathQuery = "/*[local-name()='myFields']/*[local-name()='Attachments']";
XmlNodeList myNodeList = ipForm.SelectNodes(xpathQuery);
你的XML看起来很奇怪。
为什么附件不包含两个节点?!
而且,即使我没有真正很好InfoPath,什么是'myFields'节点你正在寻找它似乎不存在于你的XML样本。
希望对你有帮助