i had to develop in my company a application that display the names and telephone number of our VoIP central.
Acessing directly a url with certains parameters (http://<ip-address>/ccmcip/xmldirectorylist.jsp?f=zita) i get a XML with the results, something like that:
1st problem: The XML have a whitespace at the begining and giving me this error:
Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it. Line 2, position 5.
I cannot changed it because its not mine.
What it's the best aproach to correct this?
The necessary result it a clean table with the names and numbers!
Note: I'm trying to use XmlTextReader, but a exception is through even the read is called.
According to your description, my understanding is that you are having difficulty with parsing the xml, and you have got an error which caused by white space.
If so, please try to refer to the following solution:
Using the TrimStart() method to remove the space.
The following code is sample, please try to refer it:
string xml = " <?xml version=\"1.0\" ?><CiscoIPPhoneDirectory><DirectoryEntry><Name>Correia, Zita</Name><Telephone>10509</Telephone></DirectoryEntry><Prompt>Records 1 to 1 of 1</Prompt><SoftKeyItem><Name>Dial</Name><URL>SoftKey:Dial</URL><Position>1</Position></SoftKeyItem><SoftKeyItem><Name>EditDial</Name><URL>SoftKey:EditDial</URL><Position>2</Position></SoftKeyItem><SoftKeyItem><Name>Exit</Name><URL>SoftKey:Exit</URL><Position>3</Position></SoftKeyItem><SoftKeyItem><Name>Search</Name><URL>http://192.168.1.1/ccmcip/xmldirectoryinput.jsp?lfzitan</URL><Position>5</Position></SoftKeyItem></CiscoIPPhoneDirectory>";
XDocument doc = XDocument.Parse(xml.TrimStart());
var xmls = doc.XPathSelectElements("CiscoIPPhoneDirectory/DirectoryEntry");
Hope it can help you. If you have any question, please let me know.
thanks for your anwser, but my problem doesn't arrive this step, I cannot get the xml result of this URL to another object (string, datatable, etc)!
I tried several ways but i allways get error: (Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it. Line 2, position 5.)
Const URLString As String = "http://<ipaddress>/ccmcip/xmldirectorylist.jsp?f=tiago"
Dim reader As XmlTextReader = New XmlTextReader(URLString)
Do While (reader.Read())
Select Case reader.NodeType
Case XmlNodeType.Element 'Display beginning of element.
Response.Write("<" + reader.Name)
If reader.HasAttributes Then 'If attributes exist
While reader.MoveToNextAttribute()
'Display attribute name and value.
Response.Write(" {0}='{1}'", reader.Name, reader.Value)
End While
End If
Response.Write("><br>")
Case XmlNodeType.Text 'Display the text in each element.
Response.Write(reader.Value)
Case XmlNodeType.EndElement 'Display end of element.
Response.Write("</" + reader.Name)
Response.Write("><br>")
End Select
Loop
Second time routine hit the while, the error is fired.
How can i read the XML result to a string or something else, and remove the whitespace later?
I think we can get
the value via the url, and then process then converted it to xml.
The following codeis using C# to get WebService vlaue, please try to refer to it:
Public Shared Function QueryPostWebService(URL As [String], MethodName As [String], Pars As Hashtable) As XmlDocument
Dim request As HttpWebRequest = DirectCast(HttpWebRequest.Create(Convert.ToString(URL) & "/" & Convert.ToString(MethodName)), HttpWebRequest)
request.Method = "POST"
request.ContentType = "application/x-www-form-urlencoded"
SetWebRequest(request)
Dim data As Byte() = EncodePars(Pars)
WriteRequestData(request, data)
Dim sr As New StreamReader(request.GetResponse().GetResponseStream(), Encoding.UTF8)
Dim retXml As [String] = sr.ReadToEnd()
'you can process your code to solve the problem.
sr.Close()
Dim doc As New XmlDocument()
doc.LoadXml(retXml)
Return doc
End Function
Member
3 Points
20 Posts
Call Manager - Getting XML with white space
Aug 30, 2013 12:41 PM|PauloFC|LINK
Hello,
i had to develop in my company a application that display the names and telephone number of our VoIP central.
Acessing directly a url with certains parameters (http://<ip-address>/ccmcip/xmldirectorylist.jsp?f=zita) i get a XML with the results, something like that:
1st problem: The XML have a whitespace at the begining and giving me this error:
I cannot changed it because its not mine.
What it's the best aproach to correct this?
The necessary result it a clean table with the names and numbers!
Note: I'm trying to use XmlTextReader, but a exception is through even the read is called.
Thanks.
Star
12777 Points
1635 Posts
Re: Call Manager - Getting XML with white space
Sep 02, 2013 06:12 AM|Terry Guo - MSFT|LINK
Hi PauloFC,
According to your description, my understanding is that you are having difficulty with parsing the xml, and you have got an error which caused by white space.
If so, please try to refer to the following solution:
Hope it can help you. If you have any question, please let me know.
Best Regards,
Terry Guo
Member
3 Points
20 Posts
Re: Call Manager - Getting XML with white space
Sep 02, 2013 07:01 AM|PauloFC|LINK
Hello Terry,
thanks for your anwser, but my problem doesn't arrive this step, I cannot get the xml result of this URL to another object (string, datatable, etc)!
I tried several ways but i allways get error: (Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it. Line 2, position 5.)
Second time routine hit the while, the error is fired.
How can i read the XML result to a string or something else, and remove the whitespace later?
Thanks.
Star
12777 Points
1635 Posts
Re: Call Manager - Getting XML with white space
Sep 03, 2013 05:44 AM|Terry Guo - MSFT|LINK
Hi Paulo,
I think we can get the value via the url, and then process then converted it to xml.
The following code is using C# to get WebService vlaue, please try to refer to it:
Hope it can help you.
Best Regards,
Terry Guo
Member
3 Points
20 Posts
Re: Call Manager - Getting XML with white space
Sep 03, 2013 08:19 AM|PauloFC|LINK
Hello Terry,
your answer does not completely resolve my problem, but gave me some lights to get me into the solution!
Thanks very much!
Best Regards
Paulo
None
0 Points
1 Post
Re: Call Manager - Getting XML with white space
Nov 25, 2013 09:07 AM|SreeKis|LINK
It really worked. Thanks for your help!