c# - XmlDocument.SelectNodes不返回InfoPath XML的节点

标签 c# xml xpath xmldocument infopath2010

我正在尝试查询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();
        }
    }
}

关于c# - XmlDocument.SelectNodes不返回InfoPath XML的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31655856/

相关文章:

python - 如何使用Python Selenium webdriver获取li内span的值?

c# - 解析 JSON 结果 - C#

c# - 在打开的cmd中运行命令

c# - 如何使用ZedGraph绘制方波?

Java Sax解析复杂的大型XML文件

java - 服务内的((按钮) View )不起作用

c# - 如何生成唯一的 12 位数字?

java - TextView 不在应用程序中居中,但在 match_constraint 中居中

xpath - Katalon-无法读取SVG

Ruby XPath 查找属性