Question : Issue Reading FOR XML Output From SQL 2005 Query

Alright, I am stuck.  I am having an issue with the following code.  The issue is: 'Unexpected end of file has occurred. The following elements are not closed: pageLink, SubPage, SubPages, Page, Pages. Line 1, position 2034.'

Now, when I run the query in SQL Manager, I get a valid XML document returned.  

What am I doing wrong?

(following is the CODE:3rd, QUERY in SQL Manager:2nd, RESULTS from SQL Manager:1st)
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
Public Function DisplaySiteMap() As String
            Dim tmpString As New StringBuilder
            Dim objDB As New ZipCM.Database
            Dim objSet As New ZipCM.Settings
            Dim strXml As String = ""
            objSet.SiteSettings()
            With objDB
                .ConnString = Common.ConnString
                .CmdType = 1
                .Query = "Select a.pageID, a.pageTitle, a.pageLink, " & _
                        "(Select b.pageID, b.pageTitle, b.pageLink From MainSitePages b Where b.parentID = a.pageID For XML Path('SubPage'), Type) As SubPages " & _
                        "From MainSitePages a " & _
                        "Where a.siteID = @SiteID " & _
                        "For XML Path('Page'), Root('Pages')"
                .ParamNames = New String() {"@SiteID"}
                .Values = New String() {objSet.SiteID}
                .ExecuteDataReader()
                If .TotalRecords > 0 Then
                    strXml = .ReturnValues(0, 0)(1)
                Else
                    strXml = ""
                End If
            End With
            objSet = Nothing
            objDB = Nothing
            Try
                Dim objXml As New XmlDocument
                Dim objNode As XmlNode
                Dim nodeCount As Long = 0
                objXml.LoadXml(strXml)
                objNode = objXml.SelectSingleNode("//Pages")
                nodeCount = objNode.ChildNodes.Count
                objNode = Nothing
                objXml = Nothing
                tmpString.Append("<h1>" & nodeCount & " Pages</h1>")
            Catch ex As Exception
                tmpString.Append(ex.Message & "<pre>" & strXml & "</pre>")
            End Try
            If tmpString.Length > 0 Then
                Return tmpString.ToString()
            Else
                Return ""
            End If
            tmpString = Nothing
        End Function
1:
2:
3:
4:
5:
6:
Select a.pageID, a.pageTitle, a.pageLink, 
(Select b.pageID, b.pageTitle, b.pageLink From MainSitePages b 
Where b.parentID = a.pageID For XML Path('SubPage'), Type) As SubPages
From MainSitePages a 
Where a.siteID = 'D37233E4-4AFB-4DD2-960F-D13C29265116' And a.parentID = 0
For XML Path('Page'), Root('Pages')
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
<Pages>
  <Page>
    <pageID>198</pageID>
    <pageTitle>Welcome To o7th Web Design</pageTitle>
    <pageLink>Home</pageLink>
  </Page>
  <Page>
    <pageID>216</pageID>
    <pageTitle>About o7th Web Design</pageTitle>
    <pageLink>About</pageLink>
    <SubPages>
      <SubPage>
        <pageID>220</pageID>
        <pageTitle>Kevin C. Pirnie - Resume</pageTitle>
        <pageLink>Kevin's Resume</pageLink>
      </SubPage>
    </SubPages>
  </Page>
  <Page>
    <pageID>217</pageID>
    <pageTitle>Our Privacy Policy</pageTitle>
    <pageLink>Privacy</pageLink>
  </Page>
  <Page>
    <pageID>218</pageID>
    <pageTitle>Contacting o7th Web Design</pageTitle>
    <pageLink>Contact Us</pageLink>
  </Page>
  <Page>
    <pageID>219</pageID>
    <pageTitle>Services Provided</pageTitle>
    <pageLink>Services</pageLink>
  </Page>
  <Page>
    <pageID>221</pageID>
    <pageTitle>o7th Web Design Blog</pageTitle>
    <pageLink>Blog</pageLink>
  </Page>
  <Page>
    <pageID>225</pageID>
    <pageTitle>Search Results</pageTitle>
    <pageLink>Search Results</pageLink>
  </Page>
  <Page>
    <pageID>226</pageID>
    <pageTitle>o7th Web Design Sitemap</pageTitle>
    <pageLink>Sitemap</pageLink>
  </Page>
  <Page>
    <pageID>227</pageID>
    <pageTitle>o7th Web Design's Portfolio</pageTitle>
    <pageLink>Our Portfolio</pageLink>
    <SubPages>
      <SubPage>
        <pageID>228</pageID>
        <pageTitle>Internet Services Inc.</pageTitle>
        <pageLink>Internet Services Inc.</pageLink>
      </SubPage>
      <SubPage>
        <pageID>229</pageID>
        <pageTitle>Best Darn Barn</pageTitle>
        <pageLink>Best Darn Barn</pageLink>
      </SubPage>
      <SubPage>
        <pageID>230</pageID>
        <pageTitle>Zip Content Management</pageTitle>
        <pageLink>Zip Content Management</pageLink>
      </SubPage>
      <SubPage>
        <pageID>231</pageID>
        <pageTitle>A Pen Lovers Paradise</pageTitle>
        <pageLink>A Pen Lovers Paradise</pageLink>
      </SubPage>
      <SubPage>
        <pageID>232</pageID>
        <pageTitle>JMB Financial Managers</pageTitle>
        <pageLink>JMB Financial Managers</pageLink>
      </SubPage>
      <SubPage>
        <pageID>233</pageID>
        <pageTitle>Crowne Plaza - Dayton</pageTitle>
        <pageLink>Crowne Plaza - Dayton</pageLink>
      </SubPage>
      <SubPage>
        <pageID>234</pageID>
        <pageTitle>Stage Stop Motel</pageTitle>
        <pageLink>Stage Stop Motel</pageLink>
      </SubPage>
      <SubPage>
        <pageID>235</pageID>
        <pageTitle>KC Customs</pageTitle>
        <pageLink>KC Customs</pageLink>
      </SubPage>
      <SubPage>
        <pageID>236</pageID>
        <pageTitle>Vietnam Battlefield Tours</pageTitle>
        <pageLink>Vietnam Battlefield Tours</pageLink>
      </SubPage>
      <SubPage>
        <pageID>237</pageID>
        <pageTitle>ZipCM - Management Site</pageTitle>
        <pageLink>ZipCM - Management Site</pageLink>
      </SubPage>
      <SubPage>
        <pageID>238</pageID>
        <pageTitle>ESR Support</pageTitle>
        <pageLink>ESR Support</pageLink>
      </SubPage>
      <SubPage>
        <pageID>239</pageID>
        <pageTitle>Showket Vineyards Emailer</pageTitle>
        <pageLink>Showket Vineyards Emailer</pageLink>
      </SubPage>
      <SubPage>
        <pageID>240</pageID>
        <pageTitle>Novato High Alumni</pageTitle>
        <pageLink>Novato High Alumni</pageLink>
      </SubPage>
      <SubPage>
        <pageID>241</pageID>
        <pageTitle>VIP Trucks</pageTitle>
        <pageLink>VIP Trucks</pageLink>
      </SubPage>
      <SubPage>
        <pageID>242</pageID>
        <pageTitle>Ink Fanatic</pageTitle>
        <pageLink>Ink Fanatic</pageLink>
      </SubPage>
      <SubPage>
        <pageID>243</pageID>
        <pageTitle>Our Tiny Family</pageTitle>
        <pageLink>Our Tiny Family</pageLink>
      </SubPage>
      <SubPage>
        <pageID>244</pageID>
        <pageTitle>Rock n Blues By The Lake</pageTitle>
        <pageLink>Rock n Blues By The Lake</pageLink>
      </SubPage>
      <SubPage>
        <pageID>245</pageID>
        <pageTitle>EPlan LLC</pageTitle>
        <pageLink>EPlan LLC</pageLink>
      </SubPage>
    </SubPages>
  </Page>
</Pages>

Answer : Issue Reading FOR XML Output From SQL 2005 Query

I solved this issue by going back to my original method and converting the output from the stored procedure to NVarCar(Max)
Random Solutions  
 
programming4us programming4us