使用 XmlDocument C# 时遇到问题
我无法让我的程序正确读取此 XML 文件,它也需要写入它,但还没有。请注意,这只是代码的一小部分,
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 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");
我还取出了 try 语句,这样我就不必添加 catch
下面是它尝试读取(然后写入)的 XML 文件
<?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>
代码运行,但是它有一个 NullReference 异常
InstalledListNodes.Attributes["title"].InnerText.Equals(packagename) == true
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 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
仅供参考,与流行的看法相反:对于属性或元素,
InnerText
永远不会null
。这意味着您根本不必检查InnerText
是否为空。空元素和属性的InnerText
为空字符串:但是,如果属性本身不存在,则可以返回
null
。正如 jrista 已经指出的那样,除非确实需要,否则使用InnerText
是没有用的。请改为使用Value
。解决您的问题
许多人已经对此发表了评论。您有:
对于您显示的 XML,这永远不会起作用,因为
没有属性。尝试:这(还)不会给出您想要的效果,但是
someNode
现在指向一个实际保存 title 属性的节点,向您展示如何消除此错误。更简单的解决方案:SelectNodes
删除错误后,我想向您展示另一种方法:XPath。使用 XPath 确实可以更轻松地完成此类任务。这是我对您的问题的看法(未经测试):
重要说明:其他人所说的有关检查节点步骤的返回值的内容仍然非常重要。如果您的任何输入数据不存在,您的代码将严重失败。只需始终检查每一步,或者使用更多 XPath 来让您的生活更轻松。
更新:仅供参考
更新:添加解决方案
更新:添加了替代解决方案(无法抗拒)
FYI and contrary to popular believe:
InnerText
is nevernull
for attributes or elements. That means, that you don't have to check forInnerText
being null at all. Empty elements and attributes have an empty string forInnerText
:However, the attribute itself can return
null
if it doesn't exist. And it is useless, as was already pointed out by jrista, to useInnerText
unless you really have to. Stick toValue
instead.Solving your issue
Many have already commented on that. You have:
with the XML you showed, this will never work, as
<installed>
does not have attributes. Try: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):
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)
似乎是一个小错字;
请注意,您编写了
Attributes["unintallername"]
,它应该是Attributes["uninstallername"]
Seems a small typo;
Note you wrote
Attributes["unintallername"]
and it should beAttributes["uninstallername"]
好吧,我可以在下面一行中看到三个不同的故障点:
我会将长链分解为更多离散元素,并检查每个元素是否为 null 以缩小范围:
也就是说,我将使用 .Value检索属性值时,使用 属性而不是 .InnerText 属性。从元素打开和关闭标记内检索文本内容时,应使用 InnerText。
Well, I can see three different failure points in the following line:
I would break that long chain up into more discrete elements, and check for null on each one to narrow it down:
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.