vb.net和xml基于前一节点的innertext选择节点

时间:2011-11-01 18:32:39

标签: asp.net xml vb.net

我的XML是这样的:

<Document type="ContentPage">
<Fields>
    <Field name="FaqCategory" type="dropdown" title="Select Category:" index="FaqCategory" list="\Lists\FaqCategory" required="true">
        Online Experience
    </Field>
    <Field name="FaqSubCategory" type="dropdown" title="Select Sub Category" list="\Lists\FaqSubCategory" required="true">
        Using the site
    </Field>
    <Field name="FaqQuestion" type="text" title="Enter FAQ Question:" required="true">
        How do I find articles on the site?
    </Field>
    <Field name="FaqAnswer" type="richtext" title="Enter FAQ Answer:" editorProfile="Advanced" required="true">
        Answer to: How do I find articles on the site?
    </Field>
</Fields>
<Placeholders />
<Indexes />

这些都在XML对象的数组中,我需要对数组进行排序并设置(显然)这个站点的FAQ部分。我遇到的问题是,由于这不是1 xml文档而是xml文档的集合,因此我很难按主要类别&gt;进行排序/分离。子类别。

这是我到目前为止获得主要类别的地方:

    Protected Sub getFaqData(ByVal DocArray() As CMSWS.CMSDocumentRecord)
    Dim mainFaqCategoryItem As String = ""
    Dim mainCategoryList As New List(Of String)
    For Each Doc As CMSWS.CMSDocumentRecord In DocArray
        xml.LoadXml(Doc.Xml)
        Try
            mainFaqCategoryItem = CType(xml.SelectSingleNode("//Fields/Field[@name='FaqCategory']").InnerText, String)
            If Not mainCategoryList.Contains(mainFaqCategoryItem) Then
                mainCategoryList.Add(mainFaqCategoryItem)
            End If
        Catch ex As Exception
            Response.Write("ERROR getting the main category")
        End Try
    Next

    For Each item In mainCategoryList
        outputsubs(item, DocArray)
    Next
End Sub

以下是获取子类别的开始以及我遇到的问题:

    Protected Sub outputsubs(ByVal item As String, ByVal DocArray() As CMSWS.CMSDocumentRecord)
    Dim subFaqCategoryItem As String = ""
    Dim subcategoryList As New List(Of String)
    'Add the main Category to the HTML output:
    lblFaq.Text = lblFaq.Text & "<h1>" & item & "</h1>"

    For Each Doc As CMSWS.CMSDocumentRecord In DocArray
        xml.LoadXml(Doc.Xml)
        Try
            'If xml.SelectSingleNode("//Fields/Field[@name='FaqCategory']").InnerText = item Then
            subFaqCategoryItem = CType(xml.SelectSingleNode("//Fields/Field[@name='FaqCategory']").InnerText, String)
            If Not subcategoryList.Contains(subFaqCategoryItem) Then
                subcategoryList.Add(subFaqCategoryItem)
            End If
            'End If

        Catch ex As Exception
            Response.Write("ERROR getting the main category")
        End Try
    Next
    For Each subItem In subcategoryList
        lblFaq.Text += "<h2><a name=""subCategory"" style=""text-decoration:none;""><span class=""FAQPlusMinus""> + </span>" & subItem & "</a></h2>"
    Next
End Sub

我的想法是,我需要设置for-each循环嵌套到大约3个级别才能使其工作,但我无法理解它。特别是因为它不是单个XML文档。 哪个用于显示主要类别

1 个答案:

答案 0 :(得分:0)

我通过反复试验找到了解决方案,不确定是否有其他人会处理类似的情况,但这是我如何让它工作:

Protected Sub outputFaqData(ByVal DocArray() As CMSWS.CMSDocumentRecord)

    Dim mainFaqCategoryItem As String = ""
    Dim mainCategoryList As New List(Of String)
    Dim xmlDocText As New StringBuilder()
    xmlDocText.Append("<Documents>")
    For Each cmsDoc As CMSWS.CMSDocumentRecord In DocArray
        xmlDocText.Append(cmsDoc.Xml)
    Next
    xmlDocText.Append("</Documents>")
    Dim doc As New XmlDocument()
    doc.LoadXml(xmlDocText.ToString())
    Dim nav As XPathNavigator = doc.CreateNavigator()
    Dim exp As XPathExpression = nav.Compile("//Documents/Document/Fields")
    exp.AddSort("Field[@name='FaqCategory']", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Text)
    exp.AddSort("Field[@name='FaqSubCategory']", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Text)
    Dim leftNavHTML As New StringBuilder()
    Dim faqHTML As New StringBuilder()
    Dim currentCategoryName As String = ""
    Dim currentFaqSubCategory As String = ""
    Dim isFirstPass As Boolean = True
    For Each item As XPathNavigator In nav.Select(exp)
        Dim FaqCategory As String = item.SelectSingleNode("Field[@name='FaqCategory']").InnerXml
        Dim FaqSubCategory As String = item.SelectSingleNode("Field[@name='FaqSubCategory']").InnerXml
        Dim FaqQuestion As String = item.SelectSingleNode("Field[@name='FaqQuestion']").InnerXml
        Dim FaqAnswer As String = item.SelectSingleNode("Field[@name='FaqAnswer']").InnerXml
        Dim isNewFaqCategory As Boolean = False
        Dim isNewFaqSubCategory As Boolean = False
        If Not currentCategoryName.Equals(FaqCategory) Then
            isNewFaqCategory = True
        End If
        If Not currentFaqSubCategory.Equals(FaqSubCategory) Then
            isNewFaqSubCategory = True
        End If
        If Not isFirstPass And (isNewFaqSubCategory Or isNewFaqCategory) Then
            faqHTML.Append("</div>")
        End If
        If isNewFaqCategory Then
            currentCategoryName = FaqCategory
            If Not isFirstPass Then
                faqHTML.Append("</div><div id=""alp_rightcolumn"">")
            End If
            leftNavHTML.Append("<h4 class='FaqCategory'>" & FaqCategory & "</h4>")
            faqHTML.Append("<h1 class='FaqCategory'>" & FaqCategory & "</h1>")
        End If
        If Not isFirstPass And (isNewFaqSubCategory Or isNewFaqCategory) Then
            faqHTML.Append("</div>")
        End If
        If isNewFaqSubCategory Then
            faqHTML.Append("<div class='FaqSubCategoryDiv'>")
        End If
        Dim QAID As String = Guid.NewGuid().ToString
        If isNewFaqSubCategory Then
            currentFaqSubCategory = FaqSubCategory
            leftNavHTML.Append("<div class='FaqSubCategory'><a href='#" & QAID & "'>" & FaqSubCategory & "</a></div>")
            'faqHTML.Append("<br/>:FIN")
            faqHTML.Append("<h2 class='FaqSubCategory'><a name='" & QAID & "'><span class='FAQPlusMinus'> + </span> " & FaqSubCategory & "</a></h2>")
        End If
        If isNewFaqSubCategory Then
            faqHTML.Append("<div class='QuestionsBox'>")
        End If
        faqHTML.Append("<a class='FAQQuestion' href='#' onClick=""showAnswer('" & QAID & "');return false;"">" & FaqQuestion & "<img src='/images/FAQ_Arrow.gif'/> </a>")
        faqHTML.Append("<div class='FAQAnswer " & QAID & "'>" & FaqAnswer & "</div>")
        isFirstPass = False
    Next
    faqHTML.Append("</div>")
    faqHTML.Append("</div>")
    litFAQLeftColumn.Text = leftNavHTML.ToString
    litFAQ.Text = faqHTML.ToString

End Sub