XMLReader ReadInnerXml会产生意外结果

时间:2015-05-22 22:58:48

标签: c# c#-4.0 xmlreader

(.Net 4.5.2 64-bit)

尝试使用XmlReader从IASE(http://iase.disa.mil/stigs/Documents/U_MicrosoftOutlook2007_V4R13_STIG.zip)解析Outlook 2007 STIG。

我遇到了XmlReader的ReadInnerXml()函数的一个小问题,我不明白(请注意" path"是包含xml文件的路径在引用的zip中:

using (var sr = new StreamReader(path))
{
  using (XmlReader reader = XmlReader.Create(sr))
  {
    while (reader.Read())
    {
      if (reader.Name.Equals("Rule") && reader.NodeType != XmlNodeType.EndElement)
      {
        Console.WriteLine("Found rule: " + reader.GetAttribute("id"));
      }
    } 
  }
}

上面的代码返回以下ID列表,这是预期的:

Found rule: SV-18181r1_rule
Found rule: SV-18188r1_rule
Found rule: SV-18203r1_rule
Found rule: SV-18602r1_rule
Found rule: SV-18213r1_rule
Found rule: SV-35249r3_rule
Found rule: SV-18641r1_rule
Found rule: SV-18655r1_rule
Found rule: SV-18657r1_rule
Found rule: SV-18663r1_rule
Found rule: SV-18667r1_rule
Found rule: SV-18671r1_rule
Found rule: SV-18673r1_rule
Found rule: SV-18675r1_rule
Found rule: SV-18677r1_rule
Found rule: SV-18679r1_rule
Found rule: SV-18681r1_rule
Found rule: SV-18683r1_rule
Found rule: SV-18685r1_rule
Found rule: SV-18687r1_rule
Found rule: SV-18689r1_rule
Found rule: SV-18708r1_rule
Found rule: SV-18710r1_rule
Found rule: SV-18712r1_rule
Found rule: SV-18729r1_rule
Found rule: SV-18731r1_rule
Found rule: SV-18735r1_rule
Found rule: SV-18743r1_rule
Found rule: SV-18749r1_rule
Found rule: SV-18752r1_rule
Found rule: SV-18766r1_rule
Found rule: SV-18775r3_rule
Found rule: SV-18779r3_rule
Found rule: SV-18838r1_rule
Found rule: SV-18840r1_rule
Found rule: SV-62707r1_rule
Found rule: SV-18842r1_rule
Found rule: SV-18844r1_rule
Found rule: SV-18846r1_rule
Found rule: SV-18848r1_rule
Found rule: SV-18850r1_rule
Found rule: SV-18852r1_rule
Found rule: SV-18910r1_rule
Found rule: SV-18912r1_rule
Found rule: SV-18916r2_rule
Found rule: SV-18918r1_rule
Found rule: SV-18920r1_rule
Found rule: SV-18935r1_rule
Found rule: SV-18946r1_rule
Found rule: SV-18948r1_rule
Found rule: SV-18950r2_rule
Found rule: SV-18958r1_rule
Found rule: SV-18960r1_rule
Found rule: SV-18962r1_rule
Found rule: SV-18964r1_rule
Found rule: SV-18970r1_rule
Found rule: SV-18978r1_rule
Found rule: SV-18980r1_rule
Found rule: SV-18985r1_rule
Found rule: SV-18988r1_rule
Found rule: SV-18990r1_rule
Found rule: SV-18992r1_rule
Found rule: SV-18995r1_rule
Found rule: SV-19005r1_rule
Found rule: SV-19010r1_rule
Found rule: SV-19012r1_rule
Found rule: SV-19014r1_rule
Found rule: SV-19018r1_rule
Found rule: SV-19023r1_rule
Found rule: SV-19026r1_rule
Found rule: SV-19028r1_rule
Found rule: SV-19030r1_rule
Found rule: SV-19032r1_rule
Found rule: SV-19038r1_rule
Found rule: SV-19040r1_rule
Found rule: SV-19042r1_rule
Found rule: SV-19050r1_rule
Found rule: SV-19435r1_rule

但是,将代码更改为:

using (var sr = new StreamReader(path))
{
  using (XmlReader reader = XmlReader.Create(sr))
  {
    while (reader.Read())
    {
      if (reader.Name.Equals("Rule") && reader.NodeType != XmlNodeType.EndElement)
      {
        Console.WriteLine("Found rule: " + reader.GetAttribute("id"));
        reader.ReadInnerXml();
      }
    }
  } 
}

将结果更改为:

Found rule: SV-18181r1_rule
Found rule: SV-18188r1_rule
Found rule: SV-18203r1_rule
Found rule: SV-18602r1_rule
Found rule: SV-18213r1_rule
Found rule: SV-35249r3_rule
Found rule: SV-18641r1_rule
Found rule: SV-18655r1_rule
Found rule: SV-18657r1_rule
Found rule: SV-18663r1_rule
Found rule: SV-18667r1_rule
Found rule: SV-18671r1_rule
Found rule: SV-18673r1_rule
Found rule: SV-18675r1_rule
Found rule: SV-18677r1_rule
Found rule: SV-18679r1_rule
Found rule: SV-18681r1_rule
Found rule: SV-18683r1_rule
Found rule: SV-18685r1_rule
Found rule: SV-18687r1_rule
Found rule: SV-18689r1_rule
Found rule: SV-18708r1_rule
Found rule: SV-18710r1_rule
Found rule: SV-18712r1_rule
Found rule: SV-18729r1_rule
Found rule: SV-18731r1_rule
Found rule: SV-18735r1_rule
Found rule: SV-18743r1_rule
Found rule: SV-18749r1_rule
Found rule: SV-18752r1_rule
Found rule: SV-18766r1_rule
Found rule: SV-18775r3_rule
Found rule: SV-18779r3_rule
Found rule: SV-18838r1_rule
Found rule: SV-18840r1_rule
Found rule: SV-18842r1_rule
Found rule: SV-18844r1_rule
Found rule: SV-18846r1_rule
Found rule: SV-18848r1_rule
Found rule: SV-18850r1_rule
Found rule: SV-18852r1_rule
Found rule: SV-18910r1_rule
Found rule: SV-18912r1_rule
Found rule: SV-18916r2_rule
Found rule: SV-18918r1_rule
Found rule: SV-18920r1_rule
Found rule: SV-18935r1_rule
Found rule: SV-18946r1_rule
Found rule: SV-18948r1_rule
Found rule: SV-18950r2_rule
Found rule: SV-18958r1_rule
Found rule: SV-18960r1_rule
Found rule: SV-18962r1_rule
Found rule: SV-18964r1_rule
Found rule: SV-18970r1_rule
Found rule: SV-18978r1_rule
Found rule: SV-18980r1_rule
Found rule: SV-18985r1_rule
Found rule: SV-18988r1_rule
Found rule: SV-18990r1_rule
Found rule: SV-18992r1_rule
Found rule: SV-18995r1_rule
Found rule: SV-19005r1_rule
Found rule: SV-19010r1_rule
Found rule: SV-19012r1_rule
Found rule: SV-19014r1_rule
Found rule: SV-19018r1_rule
Found rule: SV-19023r1_rule
Found rule: SV-19026r1_rule
Found rule: SV-19028r1_rule
Found rule: SV-19030r1_rule
Found rule: SV-19032r1_rule
Found rule: SV-19038r1_rule
Found rule: SV-19040r1_rule
Found rule: SV-19042r1_rule
Found rule: SV-19050r1_rule
Found rule: SV-19435r1_rule

当我在每条规则上调用ReadInnerXml()时,有人可以解释为什么缺少SV-62707r1_rule吗?更好的是,有人可以描述如何获取所有Rule元素的内部XML字符串而不跳过其中一个吗?

2 个答案:

答案 0 :(得分:1)

在循环中,每次迭代都会调用reader.Read()。因此,当您点击Rule开放元素标记时,您调用reader.ReadInnerXml()方法,该方法读取整个Rule元素,包括结束元素标记。在此之后,您调用reader.Read()并跳过文档中的下一个节点,如果下一个节点是另一个Rule打开元素标记,那么您会错过它。作为简单的修复,您可以在第二个代码中将if更改为while

答案 1 :(得分:0)

查看XML文件(U_MicrosoftOutlook2007_V4R13_Manual-xccdf.xml)时,您会发现该信息附带此信息:

  

标准:如果值BlockExtContent是REG_DWORD = 1,则不是a   发现。

     

在HTML电子邮件中显示外部内容和图片 - Outlook。

     

恶意电子邮件发件人可以发送带有嵌入式的HTML电子邮件   网站信标,它们是来自外部的图片和其他内容   可用于跟踪特定收件人是否打开的服务器   信息。查看包含Web信标的电子邮件   确认收件人的电子邮件地址有效,哪个   使收件人容易受到其他垃圾邮件和有害电子邮件的攻击。   默认情况下,Outlook不会下载HTML电子邮件中的外部内容   除非内容被认为是安全的,否则和RSS项目。内容   可以将Outlook 2007配置为考虑安全包括:

     

•发件人和安全发件人和安全收件人列表中定义的收件人的电子邮件中的内容。

     

•Internet Explorer的“受信任的站点”安全区域中的网站内容。

     

•RSS项目中的内容。

     

•SharePoint讨论区的内容。

     

用户可以通过更改信任中心的“自动下载”部分中的选项来控制哪些内容被视为安全。如果覆盖了Outlook的默认阻止配置,则在信任中心或其他方法中,Outlook将在所有HTML电子邮件中显示外部内容,包括任何包含Web信号的内容。

你可以从中找出问题所在。