Click here to Skip to main content
15,445,242 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
tag information using xpath.

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

XmlDocument doc = new XmlDocument();

           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=

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>
        <ProviderReference culture="en">
        <SoftwareUpdateGroup id="9215ff71-38c0-416a-b89a-fe3474160f41">
                <SoftwareUpdate culture="en">
                        <PatchFile id="1">
                            <Description>Adobe Reader 8.3 update - multiple languages</Description>
                            <CommandLine> </CommandLine>
                            <BatchFileName> </BatchFileName>
                            <BatchFileURL>http://localhost/downloads/ </BatchFileURL>
                            <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></version></regKeyPathFileVersion></expression></and></expression></installed></detection>]]></IsApplicableRule>
                            <RebootRequired> </RebootRequired>
                                        <BulletinId> </BulletinId>
                                        <UpdateName> </UpdateName>
                            <PreRequisite GroupID="1">
                                    <Name>Adobe Reader 8.0.0 - English</Name>
                                    <InventoryRuleXml> </InventoryRuleXml>
                                    <Name>Adobe Reader 8.0.0 Gold - English</Name>
                                    <InventoryRuleXml>   </InventoryRuleXml>
Updated 11-Jan-12 19:22pm

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

XmlDocument doc = new XmlDocument();
//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


Share this answer
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???????
Share this answer
Sujeet Pardeshi 12-Jan-12 6:09am    
But the mentioned solution is not working for above xml.
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
Sujeet Pardeshi 12-Jan-12 8:32am     CRLF
Thanks for the reply.... After removing reference its working perfectly fine....:):)
CPallini 12-Jan-12 8:38am     CRLF
You are welcome.
Sujeet Pardeshi 13-Jan-12 1:04am     CRLF
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.

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