Groups | Blog | Home
all groups > dotnet xml > january 2006 >

dotnet xml : Problems Reading XML File



jsh02_nova NO[at]SPAM hotmail.com
1/16/2006 8:31:04 AM
John,
Where's the declaration line? You go to have '<?xml version="1.0"
encoding="UTF-8"?>' at least, on the first line of the xml file to make it
for a valid.

thx
-jsh

[quoted text, click to view]
John Bowman
1/16/2006 8:41:17 AM
Hi All,

I'm fairly new to XML, so I presume I'm doing something obviously dense
here, but here it goes. Below is an XML file that was extracted from a test
Windows Installer .MSP (patch) file using the MsiExtractPatchXMLData()
method and directly written to disk as a test. On the surface, it seems to
me to be valid XML since IE can load & display it.

<MsiPatch xmlns="http://www.microsoft.com/msi/patch_applicability.xsd"
SchemaVersion="1.0.0.0" PatchGUID="{2AAC52C8-0F60-481F-852D-116CDE7EBE0D}"
MinMsiVersion="3">
<TargetProduct MinMsiVersion="200">
<TargetProductCode
Validate="true">{C2E2F155-71DD-458A-966B-3C231CFB5E7D}</TargetProductCode>
<TargetVersion Validate="true" ComparisonType="LessThanOrEqual"
ComparisonFilter="MajorMinorUpdate">1.00.0000</TargetVersion>
<TargetLanguage Validate="false">1033</TargetLanguage>
<UpdatedLanguages>1033</UpdatedLanguages>
<UpgradeCode
Validate="true">{1C18A718-D408-4D74-B2C6-0F6A932CE841}</UpgradeCode>
</TargetProduct>
<TargetProductCode>{C2E2F155-71DD-458A-966B-3C231CFB5E7D}</TargetProductCode>
<SequenceData>
<PatchFamily>C2E2F15571DD458A966B3C231CFB5E7D</PatchFamily>
<Sequence>0.0.17219.55500</Sequence>
<Attributes>0</Attributes>
</SequenceData>
</MsiPatch>

Now that I can actually "see" the XML for testing purposes I want to extract
some information from it, but it always fails to get anything. For example,
what's funtionally wrong with the following code? It throws an exception:
"Object reference not set to an instance of an object." when it hits the
MessageBox.Show line. When I check the LocalName of xRoot, it correctly
shows "MsiPatch", but fails to get the xTargetProductNode, or any other
nodes below the root node. Using SelectNodes to get an XmlNodeList doesn't
work either. The only way I can seem to get the info is by looping through
the ChildNodes (and comparing their LocalNames to the tag I'm looking for)
instead of directly with a simple XPath. What am I doing wrong?

if(File.Exists(FileSpec) == true)
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(DEF_OUTPUT_FILENAME);
XmlNode xRoot = xDoc.DocumentElement;
XmlNode xTargetProductNode = xRoot.SelectSingleNode("TargetProduct");
//XmlNodeList xNodeList = xRoot.SelectNodes("TargetProduct"); ///Also fails
to find anything and the xNodeList.Count = 0;
MessageBox.Show(xTargetProductNode.LocalName);
}
else
{
MessageBox.Show("Unable to read file:" +
Environment.NewLine +
FileSpec);
}


Thanks in advance for the help,

--
John C. Bowman
Software Engineer
Thermo Electron Scientific Instruments Div.
<Remove this before reply> john.bowman@thermo.com

Scott M.
1/16/2006 12:29:17 PM
I'm not so sure about that. The line is certainly preferable, but a
well-formed XML document doesn't have to have that line.


"jsh02_nova@hotmail.com" <jsh02novahotmailcom@discussions.microsoft.com>
[quoted text, click to view]

John Bowman
1/16/2006 3:43:44 PM
Scott,

I think I have to agree with you on this one. I did not create the XML, MS
did via the API call. Simply writing the text to a disk file, as is,
resulted in the XML I posted. I just couldn't figure out how to get it to
retreive the requested node(s) correctly. Based on the Martin's reply (see
next thread message), I think I'll be able to solve it when I implement
handling the namespace using an XmlNamespaceManager.

Thanks,

John

[quoted text, click to view]

John Bowman
1/16/2006 3:43:45 PM
Martin, et al,

Thanks for the help. I think this link appears to contain the solution.

John

[quoted text, click to view]

Martin Honnen
1/16/2006 6:35:10 PM


[quoted text, click to view]


[quoted text, click to view]

The super XPath FAQ, finding elements in the default namespace:
<http://www.faqts.com/knowledge_base/view.phtml/aid/34022/fid/616>


--

Martin Honnen --- MVP XML
KDOR
1/17/2006 2:33:36 AM
Hi,

I think your problem can be solved by using an XmlNamespaceManager on your
calls to SelectSingleNode and SelectNodes
Even when the nodes you are searching for are in the default namespace, you
still need a prefix, and whenever you use a prefix in an XPath expression,
you need an XmlNamespaceManager.
(See class XmlNamespaceManager.AddNamespace in VS help for a description)

XmlNamespaceManager nsmgr = new XmlNamespaceManager(xDoc .NameTable);
// Create a namespace manager
nsmgr.AddNamespace("default", http://www.microsoft.com/msi/patch);
// Add the default namespace with a prefix of "default"; could be any
character string e.g. "aa"

XmlNode xTargetProductNode =
xRoot.SelectSingleNode("default:TargetProduct", nsmgr); // Use the
prefix in the XPath expression and pass the namespace manager
XmlNodeList xNodeList = xRoot.SelectNodes("default:TargetProduct",
nsmgr); // Use the prefix in the XPath expression and
pass the namespace manager

Regards


"John Bowman john.bowman@thermo.com>" <<Remove this before reply> wrote in
message news:u$iHqrqGGHA.608@TK2MSFTNGP14.phx.gbl...
[quoted text, click to view]

John Bowman
1/17/2006 11:41:57 AM
KDOR,

Thanks. Between your help & Martin's before you, I managed to make it work.

Take care,

John


[quoted text, click to view]

AddThis Social Bookmark Button