在 C# 中从 XML 访问内部文本

本文关键字:内部 文本 访问 XML 中从 | 更新日期: 2023-09-27 18:31:04

<?xml version="1.0" encoding="utf-8"?>
<?mso-infoPathSolution name="urn:schemas-microsoft-com:office:infopath:TestCRForm:-myXSD-2013-01-09T15-23-27" solutionVersion="1.0.0.285" productVersion="14.0.0.0" PIVersion="1.0.0.0" href="http://win-rskupn6mf2b:2331/TestCRForm/Forms/template.xsn"?>
<?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.3"?>
<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:xhtml="http://www.w3.org/1999/xhtml" xmlns:tns="http://microsoft.com/webservices/SharePointPortalServer/UserProfileService" xmlns:s1="http://microsoft.com/wsdl/types/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soap="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/2013-01-09T15:23:27" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xml:lang="en-US">
    <my:VendorName>Vendor Name1</my:VendorName>
    <my:Assignedto>Asigner</my:Assignedto>
    <my:SOWNumber></my:SOWNumber>
    <my:DraftStarted xsi:nil="true"></my:DraftStarted>
    <my:DateComplete xsi:nil="true"></my:DateComplete>
    <my:GLCode>Material No1</my:GLCode>
    <my:LogID>2013-09-05T22:44:09</my:LogID>
    <my:Status>New</my:Status>
    <my:ProjectDescription>
    <html xmlns="http://www.w3.org/1999/xhtml" xml:space="preserve">
      <p>​ THIS TEXT</p>
    </html>
  </my:ProjectDescription>
</my:myFields>

我能够使用以下方法访问标签内的字段:VendorNameAssignedTo

XmlNodeList nodelist1 = xml.GetElementsByTagName("my:myFields");
foreach (XmlNode node in nodelist1)
            text_name = node["my:VendorName"].InnerText;

但是我无法访问项目描述标签中的内部文本,即"此文本"

XmlNodeList nodelist1 = xml.GetElementsByTagName("my:myFields/my:ProjectDescription/html");
foreach (XmlNode node in nodelist1)
                text1 = node["p"].InnerText;

在调试模式下运行它时,我在 nodelist1 中得到 NULL。

请向我建议一种方法。

在 C# 中从 XML 访问内部文本

XmlNamespaceManager nsm = new XmlNamespaceManager(xml.NameTable);
nsm.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2013-01-09T15:23:27");
nsm.AddNamespace("x", "http://www.w3.org/1999/xhtml");
XmlNodeList nodelist1 = xml.SelectNodes("my:myFields/my:ProjectDescription/x:html",nsm);
foreach (XmlNode node in nodelist1)
    Console.WriteLine(node["p"].InnerText);

这有效:

XmlNodeList nodelist1 = xml.SelectNodes("my:myFields/my:ProjectDescription");
            foreach (XmlNode node in nodelist1)
            {
                text1 = node["html"].InnerText;
            }

它将 tag.innerText 转换为段落格式,即使在字符串 text1 中也是如此。你可以从那里修剪它。

我使用 System.Xml.Linq.XDocumentXPath 创建了一个解决方案。我认为你可以最好地使用 XPath。它解释了我认为的命名空间问题。

XPath:

using System.Xml.XPath;
var document = System.Xml.Linq.XDocument.Load(@"D:'test.xml");
var namespaceManager = new System.Xml.XmlNamespaceManager(new System.Xml.NameTable());
namespaceManager.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2013-01-09T15:23:27");
namespaceManager.AddNamespace("html", "http://www.w3.org/1999/xhtml");
var pValue = document.XPathSelectElement("/my:myFields/my:ProjectDescription/html:html/html:p", namespaceManager).Value;

XDocument:

var xml = System.Xml.Linq.XDocument.Load(@"D:'test.xml");
var myNamespace = "http://schemas.microsoft.com/office/infopath/2003/myXSD/2013-01-09T15:23:27";
var htmlNamespace = "http://www.w3.org/1999/xhtml";
var myFields = xml.Element(System.Xml.Linq.XName.Get("myFields", myNamespace));
var projectDescription = myFields.Element(System.Xml.Linq.XName.Get("ProjectDescription", myNamespace));
var htmlTag = projectDescription.Element(System.Xml.Linq.XName.Get("html", htmlNamespace));
var pTag = htmlTag.Element(System.Xml.Linq.XName.Get("p", htmlNamespace));
var pValue = pTag.Value;