无法使用xpath读取值 [英] Not able to read values using xpath

查看:158
本文介绍了无法使用xpath读取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以使用xpath读取复杂xml文件的值吗?>

考虑下面的xml文件(在问题末尾复制).我想使用xpath读取

Can we read values using xpath for complex xml file>???

Consider the below xml file (copied at the end of question). I want to read

PatchFile

标记信息.

但是当我使用下面的表达式时,它给我的结果为null,

tag information using xpath.

But when I used below expression it gives me result as null,

XmlDocument doc = new XmlDocument();
           doc.Load(@"E:\xmls\p_apsb11-16.xml");

           XmlElement root = doc.DocumentElement;
           XmlNodeList swupdatelist = root.SelectNodes("/Bulletin");



如果我使用*而不是提供名称,那么它将给我适当的结果.

例如字符串Xpath_Expression =



If I used * instead of giving name then it will give me proper result.

e.g. string Xpath_Expression=

root.SelectNodes("/*/*[4]/*/*/*");



如果我在xpath表达式中为简单的xml文件使用了标记名,则可以得到正确的结果.如果您有adea,请告诉我.如何在xpath表达式中使用标签名而不是*.

我已在下面复制了所需的Xml:-



If I used Tag name in xpath expression for simple xml file it gives me proper result. Please let me know if you have adea. How can we use tag name instead of giving * in xpath expression.

I have copied the required Xml below:-

<Bulletin vendor="Adobe" id="APSB11-16" firstReleaseDate="6/29/2011" lastRevisionDate="6/29/2011" version="1.0" severity="Critical" xmlns="Resource.xsd">
    <Description>Security updates available for Adobe Reader and Acrobat</Description>
    <Summary>For users of Adobe Reader X (10.0.1) for Windows and Macintosh, Adobe has made available the update, Adobe Reader 9.4.4.</Summary>
    <ProviderReferences>
        <ProviderReference culture="en">
            <URL>http://www.adobe.com/support/security/bulletins/apsb11-16.html</URL>
        </ProviderReference>
    </ProviderReferences>
    <SoftwareUpdateGroups>
        <SoftwareUpdateGroup id="9215ff71-38c0-416a-b89a-fe3474160f41">
            <SoftwareUpdates>
                <SoftwareUpdate culture="en">
                    <PatchFiles>
                        <PatchFile id="1">
                            <Name>AdbeRdr830_en_US.msi</Name>
                            <Description>Adobe Reader 8.3 update - multiple languages</Description>
                            <FileName>AdbeRdr830_en_US.msi</FileName>
                            <PatchSize>34661</PatchSize>
                            <DatePublished>6/29/2011</DatePublished>
                            <DownloadURL>http://ardownload.adobe.com/pub/adobe/reader/win/8.x/8.3.0/en_US/AdbeRdr830_en_US.msi</DownloadURL>
                            <CommandLine> </CommandLine>
                            <InstallationType>870171E8-A8B1-4797-ADBD-5C112AD92FFA</InstallationType>
                            <BatchFileName> </BatchFileName>
                            <BatchFileURL>http://localhost/downloads/ </BatchFileURL>
                            <InventoryRule><![CDATA[666]]></InventoryRule>
                            <IsApplicableRule><![CDATA[<detection><installed><expression><and><expression><regKeyPathFileVersion versionStatus="HIGHER_OR_SAME"><key>HKEY_LOCAL_MACHINE\SOFTWARE\Adobe\Acrobat Reader\8.0\Installer</key><entry>Path</entry><filePath name="Reader\AcroRd32.dll" /><version>8.0.0.0</version></regKeyPathFileVersion></expression></and></expression></installed></detection>]]></IsApplicableRule>
                            <RebootRequired> </RebootRequired>
                            <Supercedence>
                                <SupercededFiles>
                                    <SupercededFile>
                                        <BulletinId> </BulletinId>
                                        <UpdateName> </UpdateName>
                                    </SupercededFile>
                                </SupercededFiles>
                            </Supercedence>
                            <PreRequisite GroupID="1">
                                <Resource>
                                    <Name>Adobe Reader 8.0.0 - English</Name>
                                    <ResourceType>SR</ResourceType>
                                    <ProductName>Office</ProductName>
                                    <InventoryRuleXml> </InventoryRuleXml>
                                </Resource>
                                <Resource>
                                    <Name>Adobe Reader 8.0.0 Gold - English</Name>
                                    <ResourceType>SP</ResourceType>
                                    <ProductName>Office</ProductName>
                                    <InventoryRuleXml>   </InventoryRuleXml>
                                </Resource>
                            </PreRequisite>
                        </PatchFile>
                    </PatchFiles>
                    <InfoURL>http://www.adobe.com/support/downloads/detail.jsp?ftpID=5123</InfoURL>
                </SoftwareUpdate>
            </SoftwareUpdates>
        </SoftwareUpdateGroup>
    </SoftwareUpdateGroups>
</Bulletin>

推荐答案

//This is the Code to load your Xml file:

XmlDocument doc = new XmlDocument();
                doc.Load(@"E:\xmls\p_apsb11-16.xml");
//Extract All nodes which contains data

                var xmlCompanyNodes = doc.GetElementsByTagName("PatchFile");
               
//Iteration on Nodes
                foreach (XmlNode xmlCompanyNode in xmlCompanyNodes)
                {
// Extract all nodes which contains actual data
                    var result = xmlCompanyNode.ChildNodes;
//iteration on all nodes to get data
                    foreach (XmlNode variable in result)
                    {
// printing to check output
                        Console.WriteLine(variable.InnerText);

                       
                    }

                }


http://www.java2s. com/Code/CSharp/XML/ReadXMLnodeusingtheXMLpath.htm [在C#中使用XPathNavigator [
http://www.java2s.com/Code/CSharp/XML/ReadXMLnodeusingtheXMLpath.htm[^]


Using XPathNavigator in C#[^]


如果我删除对架构文件的引用(例如, Bulletin节点中的xmlns="Resource.xsd").
你有这样的文件吗?
It works for me if I remove the reference to the schema file (i.e. xmlns="Resource.xsd") in the Bulletin node.
Have you such a file?


这篇关于无法使用xpath读取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆