I'm having trouble getting my program to read this XML file properly, it will need to write to it also but not yet. Just note that this is only a little bit of the code
XmlDocument InstalledList = new XmlDocument();
InstalledList.Load(AppsInstalledFileNamePath);
//Sets the PackageNode to the correct part of the XmlDocument
XmlNodeList PackagesNode = InstalledList.GetElementsByTagName("installed");
foreach (XmlNode InstalledListNodes in PackagesNode)
{
//If the title 开发者_Python百科is the same as what the user typed, continue on
if (InstalledListNodes.Attributes["title"].InnerText.Equals(packagename) == true)
{
BatchProcessFileName = InstalledListNodes.Attributes["uninstallername"].InnerText;
Console.WriteLine("Filename OK");
I also took the try statement out so I wouldn't have to add the catch
Below is the XML File that it is trying to read (and later write)
<?xml version="1.0" encoding="utf-8" ?>
<packages>
<installed>
<sampleapp title="sampleapp" id="00001" uninstallername="sampleapp.bat" installdate="11/15/09"></sampleapp>
<sampleapp2 title="sampleapp2" id="00002" uninstallername="sampleapp2.bat" installdate="11/16/09"></sampleapp2>
</installed>
<uninstalled>
</uninstalled>
</packages>
The code runs, but it has a NullReference Exception at
InstalledListNodes.Attributes["title"].InnerText.Equals(packagename) == true
FYI and contrary to popular believe: InnerText
is never null
for attributes or elements. That means, that you don't have to check for InnerText
being null at all. Empty elements and attributes have an empty string for InnerText
:
XmlDocument docx = new XmlDocument();
docx.LoadXml("<root test='' />");
Debug.WriteLine("Empty: " + docx.FirstChild.InnerText);
Debug.WriteLine("Empty: " + docx.FirstChild.Attributes["test"].InnerText);
However, the attribute itself can return null
if it doesn't exist. And it is useless, as was already pointed out by jrista, to use InnerText
unless you really have to. Stick to Value
instead.
Solving your issue
Many have already commented on that. You have:
XmlNodeList PackagesNode = InstalledList.GetElementsByTagName("installed");
foreach (XmlNode InstalledListNodes in PackagesNode)
{
if (InstalledListNodes.Attributes["title"].InnerText.Equals(packagename) == true)
....
with the XML you showed, this will never work, as <installed>
does not have attributes. Try:
XmlNodeList PackagesNode = InstalledList.GetElementsByTagName("installed");
foreach (XmlNode InstalledListNodes in PackagesNode)
{
XmlNode someNode = InstalledListNodes.FirstChild;
if (someNode.Attributes["title"].InnerText.Equals(packagename) == true)
....
which will not (yet) give the effect you want, but someNode
now points to a node that actually holds the title attribute, showing you how to get rid of this error.
On to an easier solution: SelectNodes
After removing your error, I'd like to show you another way: XPath. This type of tasks is really much easier by using XPath. Here is my take at your problem (not tested):
// assuming <packages> is root:
XmlNodeList applicationNodes = InstalledList.SelectNodes("/packages/installed/*");
foreach (XmlNode applicationNode in applicationNodes)
{
if (applicationNode.Attributes["title"].Value.Equals(packagename) == true)
{
.... etc
Important note: what others have said about checking the return values of node steps is still very important. If anything of your input data is not there, your code will fail hard. Just always check every step, or use more XPath to make your life easier.
Update: the FYI
Update: added solution
Update: added alternative solution (couldn't resist)
Seems a small typo;
Note you wrote Attributes["unintallername"]
and it should be Attributes["uninstallername"]
Well, I can see three different failure points in the following line:
InstalledListNodes.Attributes["title"].InnerText.Equals(packagename) == true
^--Null? ^-- Null? ^--Null?
I would break that long chain up into more discrete elements, and check for null on each one to narrow it down:
if (InstalledListNodes != null)
{
var attribute = InstalledListNodes.Attributes["title"];
if (attribute != null)
{
if (attribute.InnerText.Equals(packagename) == true)
{
// ...
}
}
}
That said, I would use the .Value property rather than the .InnerText property when retrieving attribute values. InnerText should be used when retrieving text content from within element open and close tags.
精彩评论