Click here to Skip to main content
15,884,092 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
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");


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

e.g. string Xpath_Expression=
root.SelectNodes("/*/*[4]/*/*/*");


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:-
XML
<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>
Posted
Updated 11-Jan-12 19:22pm
v2

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?
 
Share this answer
 
Comments
Sujeet Pardeshi 12-Jan-12 8:32am    
Thanks for the reply....
After removing reference its working perfectly fine....:):)
CPallini 12-Jan-12 8:38am    
You are welcome.
Sujeet Pardeshi 13-Jan-12 1:04am    
Actually we are using Resource.xsd file to check if xml file is valid or not in our code. And today I have found solution.
In order to resolve references to schema file or namespace, we need to use XmlNamespaceManager class.

Here is the detailed code.
XPathDocument doc = new XPathDocument(@"E:\xmls\P_APSB11-21.xml");
XPathNavigator nav = doc.CreateNavigator();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nav.NameTable);
nsmgr.AddNamespace("ms", "Resource.xsd");


XPathNodeIterator PatchFilesiterator= nav.Select("//ms:PatchFiles",v);

Today I found that, if there is any reference to schema file or namespace then we need to resolve that reference using XmlNamespaceManager class.
 
Share this answer
 
Comments
Sujeet Pardeshi 12-Jan-12 6:09am    
But the mentioned solution is not working for above xml.
C#
//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);

                       
                    }

                }
 
Share this answer
 
Comments
Sujeet Pardeshi 12-Jan-12 6:05am    
Yes its possible using System.Xml namespace. But can we extract data from above xml using XPath using tagnames???????

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900