Мой VB код не работает при циклическом прохождении через второй цикл
У меня есть простой xml-документ, из которого я хочу создать электронную таблицу excel. Мой vb-код работает только тогда, когда xml-документ не имеет родительского отчета с дочерними элементами.
Как я могу изменить оператор For Each, чтобы код не выходил из строя во втором цикле, где нет следующих элементов FName, MName, LName, LogIn и Organization
Например он работает для этого xml
Я использую objStreamWriter для вывода листа excel.
имя xml файла сохраняется в objXmlResultDoc
Желаемый выход в 2 строки с 8 столбцами
FirstName MiddleName Фамилия Логин Организация OrganizationORI OrganizationName CaseNumber
Кто-то X Pati File\JeanX 999 NULL NULL 2312
Нуль Нуль Нуль Нуль Нуль 999 Googleee
2312
Мой код не работает для следующего xml-документа
<?xml version="1.0" encoding="UTF-8"?>
<Results> <Report> <FName>Someone</FName> <MName>X</MName> <LName>Pati</LName> <LogIn>File\JeanX</LogIn> <Organization>999</Organization> <CaseNumber>2312</CaseNumber> </Report> <Report> <OrganizationORI>999</OrganizationORI> <OrganizationName>Googleee</OrganizationName> <CaseNumber>2312</CaseNumber> </Report> </Results>
Vb код
'Create excel Dim objXMLNode As XmlNode Dim objXmlResultDoc As XmlDocument Dim strFName As String Dim strMName As String Dim strOrganizationName As String Dim strLName As String Dim strLogIn As String Dim strOrganization As String Dim strCaseNumber As String objXMLNode = objXmlResultDoc.DocumentElement.SelectSingleNode("Report") objOutputMemStream = New System.IO.MemoryStream objStreamWriter = New System.IO.StreamWriter(objOutputMemStream) objStreamWriter.WriteLine("Requestor First Name, Requestor Middle Name, Requestor Last Name, Requestor Login, Organization ID, Organization Name, Case Number") objXMLNode = objXmlResultDoc.DocumentElement.SelectSingleNode("Report") For Each objXMLNode In objXmlResultDoc.DocumentElement.SelectNodes("Report") 'Get FirstName If objXMLNode.SelectSingleNode("Report/"FName") Is Nothing Then strFirstName = objXMLNode.SelectSingleNode(FName").InnerText Else strFirstName = "NULL" End If 'Get MiddleName If objXMLNode.SelectSingleNode("Report/"MiddleName") Is Nothing Then strMiddleName = objXMLNode.SelectSingleNode("MiddleName").InnerText Else strMiddleName = "NULL" End If 'Get LastName If objXMLNode.SelectSingleNode("Report/"LastName") Is Nothing Then strLastName = objXMLNode.SelectSingleNode("LastName").InnerText Else strLastName = "NULL" End If 'Get Login If objXMLNode.SelectSingleNode("Report/"LogIn") Is Nothing Then strLogIn = objXMLNode.SelectSingleNode("LogIn").InnerText Else strLogIn = "NULL" End If 'Get Organization If objXMLNode.SelectSingleNode("Report/"Organization") Is Nothing Then strOrganization = objXMLNode.SelectSingleNode("Organization").InnerText Else strOrganization = "NULL" End If 'Get CaseNumber If objXMLNode.SelectSingleNode("Report/"CaseNumber") Is Nothing Then strCaseNumber = objXMLNode.SelectSingleNode(CaseNumber").InnerText Else strCaseNumber = "NULL" End If objStreamWriter.WriteLine(strFName + "," + strMName + "," + strLName + "," + strLogIn + "," + strOrganization + "," + strOrganizationName + "," + strCaseNumber) Next objStreamWriter.Flush() objOutputMemStream.Position = 1
Что я уже пробовал:
Dim strLName As String Dim strLogIn As String Dim strOrganization As String Dim strCaseNumber As String objXMLNode = objXmlResultDoc.DocumentElement.SelectSingleNode("Report") objOutputMemStream = New System.IO.MemoryStream objStreamWriter = New System.IO.StreamWriter(objOutputMemStream) objStreamWriter.WriteLine("Requestor First Name, Requestor Middle Name, Requestor Last Name, Requestor Login, Organization ID, Organization Name, Case Number") objXMLNode = objXmlResultDoc.DocumentElement.SelectSingleNode("Report") For Each objXMLNode In objXmlResultDoc.DocumentElement.SelectNodes("Report") 'Get FirstName If objXMLNode.SelectSingleNode("Report/"FName") Is Nothing Then strFirstName = objXMLNode.SelectSingleNode(FName").InnerText Else strFirstName = "NULL" End If 'Get MiddleName If objXMLNode.SelectSingleNode("Report/"MiddleName") Is Nothing Then strMiddleName = objXMLNode.SelectSingleNode("MiddleName").InnerText Else strMiddleName = "NULL" End If 'Get LastName If objXMLNode.SelectSingleNode("Report/"LastName") Is Nothing Then strLastName = objXMLNode.SelectSingleNode("LastName").InnerText Else strLastName = "NULL" End If 'Get Login If objXMLNode.SelectSingleNode("Report/"LogIn") Is Nothing Then strLogIn = objXMLNode.SelectSingleNode("LogIn").InnerText Else strLogIn = "NULL" End If 'Get Organization If objXMLNode.SelectSingleNode("Report/"Organization") Is Nothing Then strOrganization = objXMLNode.SelectSingleNode("Organization").InnerText Else strOrganization = "NULL" End If 'Get CaseNumber If objXMLNode.SelectSingleNode("Report/"CaseNumber") Is Nothing Then strCaseNumber = objXMLNode.SelectSingleNode(CaseNumber").InnerText Else strCaseNumber = "NULL" End If objStreamWriter.WriteLine(strFName + "," + strMName + "," + strLName + "," + strLogIn + "," + strOrganization + "," + strOrganizationName + "," + strCaseNumber) Next objStreamWriter.Flush() objOutputMemStream.Position = 1