Выскакивает ошибка: В документе XML (1, 2) присутствует ошибка - C#

Узнай цену своей работы

Формулировка задачи:

Возникает ошибка здесь:
Листинг программы
  1. object obj = mySerializer.Deserialize(ms);
Листинг программы
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. using (XmlReader xml = XmlReader.Create("2.xml"))
  6. {
  7. while (xml.Read())
  8. {
  9. switch (xml.NodeType)
  10. {
  11. case XmlNodeType.Element:
  12. if (xml.Name == "entry")
  13. {
  14. // передаем данные в класс Member
  15. Member m = new Member(xml.ReadOuterXml());
  16. Console.WriteLine("KUID: {0}", m.ID);
  17. Console.WriteLine("Псевдоним: {0}", m.published_datetime);
  18. Console.WriteLine("------------------------------------------");
  19. }
  20. /*else if (xml.Name == "member")
  21. {
  22. }*/
  23. break;
  24. }
  25. }
  26. }
  27. Console.ReadKey();
  28. }
  29. }
  30. [XmlRoot("entry")]
  31. public class Member
  32. {
  33. [XmlAttribute("id")]
  34. public string ID { get; set; }
  35. [XmlElement("vuln:published-datetime")]
  36. public string published_datetime { get; set; }
  37. public Member() { }
  38. public Member(string xml)
  39. {
  40. LoadXml(xml);
  41. }
  42. public void LoadXml(string source)
  43. {
  44. XmlSerializer mySerializer = new XmlSerializer(this.GetType());
  45. using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(source)))
  46. {
  47. object obj = mySerializer.Deserialize(ms); // Ошибка здесь
  48. foreach (PropertyInfo p in obj.GetType().GetProperties())
  49. {
  50. PropertyInfo p2 = this.GetType().GetProperty(p.Name);
  51. if (p2 != null && p2.CanWrite)
  52. {
  53. p2.SetValue(this, p.GetValue(obj, null), null);
  54. }
  55. }
  56. }
  57. }
  58. }
Не могу понять почему такая ошибка, помогите Пример XML моей:
Листинг программы
  1. <?xml version='1.0' encoding='UTF-8'?>
  2. <nvd xmlns:scap-core="http://scap.nist.gov/schema/scap-core/0.1" xmlns:cvss="http://scap.nist.gov/schema/cvss-v2/0.2" xmlns:vuln="http://scap.nist.gov/schema/vulnerability/0.4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:patch="http://scap.nist.gov/schema/patch/0.1" xmlns="http://scap.nist.gov/schema/feed/vulnerability/2.0" xmlns:cpe-lang="http://cpe.mitre.org/language/2.0" nvd_xml_version="2.0" pub_date="2015-11-10T03:23:57" xsi:schemaLocation="http://scap.nist.gov/schema/patch/0.1 [url]http://nvd.nist.gov/schema/patch_0.1.xsd[/url] [url]http://scap.nist.gov/schema/feed/vulnerability/2.0[/url] [url]http://nvd.nist.gov/schema/nvd-cve-feed_2.0.xsd[/url] [url]http://scap.nist.gov/schema/scap-core/0.1[/url] http://nvd.nist.gov/schema/scap-core_0.1.xsd">
  3. <entry id="CVE-2014-2406">
  4. <vuln:vulnerable-configuration id="http://www.nist.gov/">
  5. <cpe-lang:logical-test operator="OR" negate="false">
  6. <cpe-lang:fact-ref name="cpe:/a:oracle:database_server:12.1.0.1"/>
  7. <cpe-lang:fact-ref name="cpe:/a:oracle:database_server:11.2.0.4"/>
  8. <cpe-lang:fact-ref name="cpe:/a:oracle:database_server:11.2.0.3"/>
  9. <cpe-lang:fact-ref name="cpe:/a:oracle:database_server:11.1.0.7"/>
  10. </cpe-lang:logical-test>
  11. </vuln:vulnerable-configuration>
  12. <vuln:vulnerable-software-list>
  13. <vuln:product>cpe:/a:oracle:database_server:11.2.0.4</vuln:product>
  14. <vuln:product>cpe:/a:oracle:database_server:11.2.0.3</vuln:product>
  15. <vuln:product>cpe:/a:oracle:database_server:11.1.0.7</vuln:product>
  16. <vuln:product>cpe:/a:oracle:database_server:12.1.0.1</vuln:product>
  17. </vuln:vulnerable-software-list>
  18. <vuln:cve-id>CVE-2014-2406</vuln:cve-id>
  19. <vuln:published-datetime>2014-04-15T21:55:10.743-04:00</vuln:published-datetime>
  20. <vuln:last-modified-datetime>2014-04-16T13:22:08.120-04:00</vuln:last-modified-datetime>
  21. <vuln:cvss>
  22. <cvss:base_metrics>
  23. <cvss:score>8.5</cvss:score>
  24. <cvss:access-vector>NETWORK</cvss:access-vector>
  25. <cvss:access-complexity>MEDIUM</cvss:access-complexity>
  26. <cvss:authentication>SINGLE_INSTANCE</cvss:authentication>
  27. <cvss:confidentiality-impact>COMPLETE</cvss:confidentiality-impact>
  28. <cvss:integrity-impact>COMPLETE</cvss:integrity-impact>
  29. <cvss:availability-impact>COMPLETE</cvss:availability-impact>
  30. <cvss:source>http://nvd.nist.gov</cvss:source>
  31. <cvss:generated-on-datetime>2014-04-16T13:22:07.870-04:00</cvss:generated-on-datetime>
  32. </cvss:base_metrics>
  33. </vuln:cvss>
  34. <vuln:references xml:lang="en" reference_type="VENDOR_ADVISORY">
  35. <vuln:source>CONFIRM</vuln:source>
  36. <vuln:reference href="http://www.oracle.com/technetwork/topics/security/cpuapr2014-1972952.html" xml:lang="en">http://www.oracle.com/technetwork/topics/security/cpuapr2014-1972952.html</vuln:reference>
  37. </vuln:references>
  38. <vuln:summary>Unspecified vulnerability in the Core RDBMS component in Oracle Database Server 11.1.0.7, 11.2.0.3, 11.2.0.4, and 12.1.0.1 allows remote authenticated users to affect confidentiality, integrity, and availability via unknown vectors related to "Advisor" and "Select Any Dictionary" privileges.</vuln:summary>
  39. </entry>
  40. </nvd>

Решение задачи: «Выскакивает ошибка: В документе XML (1, 2) присутствует ошибка»

textual
Листинг программы
  1. using System;
  2. using System.Xml;
  3. using System.Xml.Linq;
  4. using System.Xml.Serialization;
  5.  
  6. namespace ConsoleApplication5
  7. {
  8.  
  9.     [XmlRoot(Namespace = "http://cpe.mitre.org/language/2.0")]
  10.     public class CpeLangFactRefType
  11.     {
  12.         [XmlAttribute("name")]
  13.         public string Name { get; set; }
  14.     }
  15.  
  16.  
  17.     [XmlRoot(Namespace = "http://cpe.mitre.org/language/2.0")]
  18.     public class CpeLangLogicalTestType
  19.     {
  20.         [XmlAttribute("operator")]
  21.         public string Operator { get; set; }
  22.        
  23.         [XmlAttribute("negate")]
  24.         public string Negate { get; set; }
  25.  
  26.         [XmlElement("fact-ref", typeof(CpeLangFactRefType))]
  27.         public CpeLangFactRefType[] Facts { get; set; }
  28.     }
  29.  
  30.     [XmlRoot(Namespace = "http://cpe.mitre.org/language/2.0")]
  31.     public class VulnVulnerableConfigurationType
  32.     {
  33.         [XmlAttribute("id")]
  34.         public string id { get; set; }
  35.  
  36.         [XmlElement("logical-test", typeof(CpeLangLogicalTestType))]
  37.  
  38.         public CpeLangLogicalTestType LogicalTest { get; set; }
  39.     }
  40.  
  41.  
  42.     [XmlRoot(Namespace = "http://scap.nist.gov/schema/vulnerability/0.4")]
  43.     public class VulnVulnerableSoftwareListType
  44.     {
  45.         [XmlElement("product")]
  46.         public string[] Products { get; set; }
  47.     }
  48.  
  49.     [XmlRoot(Namespace = "http://scap.nist.gov/schema/cvss-v2/0.2")]
  50.     public class CvssBaseMetricsType
  51.     {
  52.         [XmlElement("score")]
  53.         public string Score { get; set; }
  54.  
  55.         [XmlElement("access-vector")]
  56.         public string AccessVector { get; set; }
  57.  
  58.         [XmlElement("access-complexity")]
  59.         public string AccessComplexity { get; set; }
  60.  
  61.         [XmlElement("authentication")]
  62.         public string Authentication { get; set; }
  63.  
  64.         [XmlElement("confidentiality-impact")]
  65.         public string ConfidentialityImpact { get; set; }
  66.  
  67.         [XmlElement("integrity-impact")]
  68.         public string IntegrityImpact { get; set; }
  69.  
  70.         [XmlElement("availability-impact")]
  71.         public string AvailabilityImpact { get; set; }
  72.  
  73.         [XmlElement("source")]
  74.         public string Source { get; set; }
  75.  
  76.         [XmlElement("generated-on-datetime")]
  77.         public DateTime GeneratedOnDateTime { get; set; }
  78.     }
  79.  
  80.     [XmlRoot(Namespace = "http://scap.nist.gov/schema/cvss-v2/0.2")]
  81.     public class VulnCvssType
  82.     {
  83.         [XmlElement("base_metrics", typeof(CvssBaseMetricsType))]
  84.         public CvssBaseMetricsType BaseMetrics { get; set; }
  85.     }
  86.  
  87.     [XmlRoot(Namespace = "http://scap.nist.gov/schema/vulnerability/0.4")]
  88.     public class VulnReferenceType
  89.     {
  90.         [XmlAttribute("href")]
  91.         public string Href { get; set; }
  92.  
  93.         [XmlAttribute("xml:lang")]
  94.         public string XmlLang { get; set; }
  95.     }
  96.  
  97.     [XmlRoot(Namespace = "http://scap.nist.gov/schema/vulnerability/0.4")]
  98.     public class VulnReferencesType
  99.     {
  100.         [XmlAttribute("xml:lang")]
  101.         public string XmlLang { get; set; }
  102.  
  103.         [XmlAttribute("reference_type")]
  104.         public string ReferenceType { get; set; }
  105.  
  106.         [XmlElement("source")]
  107.         public string Source { get; set; }
  108.  
  109.         [XmlElement("reference", typeof(VulnReferenceType))]
  110.         public VulnReferenceType Reference { get; set; }
  111.     }
  112.  
  113.     [XmlRoot(Namespace = "http://scap.nist.gov/schema/vulnerability/0.4")]
  114.     public class EntryType
  115.     {        
  116.         [XmlAttribute("id")]
  117.         public string id { get; set; }
  118.  
  119.         [XmlElement("vulnerable-configuration", typeof(VulnVulnerableConfigurationType))]
  120.         public VulnVulnerableConfigurationType VulnerableConfiguration { get; set; }
  121.  
  122.         [XmlElement("vulnerable-software-list", typeof(VulnVulnerableSoftwareListType))]
  123.         public VulnVulnerableSoftwareListType VulnerableSoftwareList { get; set; }
  124.  
  125.         [XmlElement("cve-id")]
  126.         public string CveId { get; set; }
  127.  
  128.         [XmlElement("published-datetime")]
  129.         public DateTime PublishedDateTime { get; set; }
  130.  
  131.         [XmlElement("last-modified-datetime")]
  132.         public DateTime LastModifiedDateTime { get; set; }
  133.  
  134.         [XmlElement("cvss", typeof(VulnCvssType))]
  135.         public VulnCvssType Cvss { get; set; }
  136.  
  137.         [XmlElement("references", typeof(VulnReferencesType))]
  138.         public VulnReferencesType References { get; set; }
  139.  
  140.         [XmlElement("summary")]
  141.         public string Summary { get; set; }
  142.  
  143.     }
  144.  
  145.     [XmlRoot("nvd", Namespace = "http://scap.nist.gov/schema/feed/vulnerability/2.0")]
  146.     public class Nvd
  147.     {
  148.         public static Nvd Create(string xmlFileName)
  149.         {
  150.             var document = XDocument.Load(xmlFileName);
  151.             var xmlSerializer = new XmlSerializer(typeof(Nvd));
  152.             return (Nvd)xmlSerializer.Deserialize(document.CreateReader());
  153.         }
  154.  
  155.         [XmlAttribute("nvd_xml_version")]
  156.         public double NvdXmlVersion { get; set; }
  157.  
  158.         [XmlAttribute("pub_date")]
  159.         public DateTime PubDate { get; set; }
  160.  
  161.         [XmlElement("entry", typeof(EntryType))]
  162.         public EntryType Entry { get; set; }
  163.     }
  164.  
  165.     class Program
  166.     {
  167.         static void Main(string[] args)
  168.         {
  169.             var nvd = Nvd.Create("database.xml");
  170.         }
  171.     }
  172. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

8   голосов , оценка 4 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы