Парсинг xml страницы - C#
Формулировка задачи:
Всем доброго времени. Есть xml страница откуда нужно получить ноду из тега <Author>.
XML :
Код на с#
Компилируется но выдает ошибку - ссылка на обьект не указывает на экземпляр обьекта.
Листинг программы
- <?mso-application progid="Excel.Sheet"?>
- <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">
- <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
- <Author>Stanislav Verner</Author>
- <LastAuthor>Stanislav Verner</LastAuthor>
- <Created>2017-05-19T10:23:01Z</Created>
- <LastSaved>2017-05-19T11:59:46Z</LastSaved>
- <Version>16.00</Version>
- </DocumentProperties>
Листинг программы
- public void Go()
- {
- XmlTextReader ValueReader = new XmlTextReader("http://castle-privatesolutions.sg/assets/quotes/book1.xml");
- try
- {
- while (ValueReader.Read())
- {
- switch (ValueReader.NodeType)
- {
- case XmlNodeType.Element:
- if (ValueReader.Name == "DocumentProperties")
- {
- if (ValueReader.HasAttributes)
- {
- ValueReader.MoveToElement();
- var Author = ValueReader.ReadOuterXml();
- XmlDocument XmlDocument = new XmlDocument();
- XmlDocument.LoadXml(Author);
- XmlNode xmlNode = XmlDocument.SelectSingleNode("DocumentProperties/Author");
- MessageBox.Show(xmlNode.ToString(), "");
- }
- }
- break;
- }
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, ex.Source);
- }
- }
Решение задачи: «Парсинг xml страницы»
textual
Листинг программы
- ...
- if (ValueReader.HasAttributes)
- {
- ValueReader.MoveToElement();
- var documentPropertiesXml = ValueReader.ReadOuterXml();
- XmlDocument xmlDoc = new XmlDocument();
- var xmlnsMngr = new XmlNamespaceManager(xmlDoc.NameTable);
- xmlnsMngr.AddNamespace("msoffice", "urn:schemas-microsoft-com:office:office");
- xmlDoc.LoadXml(documentPropertiesXml);
- XmlNode xmlNode = xmlDoc.SelectSingleNode("msoffice:DocumentProperties/msoffice:Author", xmlnsMngr);
- string author = xmlNode.InnerText;
- MessageBox.Show(author, "");
- }
- ...
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д