Reputation: 811
I am working on c# web services and here I am getting string response and I have attached below exception.
Exception message:
System.Xml.XmlException: Name cannot begin with the '{' character, hexadecimal value 0x7B. Line 3, position 14.
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.Throw(String res, String[] args)
at System.Xml.XmlTextReaderImpl.Throw(Int32 pos, String res, String[] args)
at System.Xml.XmlTextReaderImpl.ParseAttributes()
at System.Xml.XmlTextReaderImpl.ParseElement()
at System.Xml.XmlTextReaderImpl.ParseElementContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace)
at System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc)
at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
at System.Xml.XmlDocument.Load(XmlReader reader)
at System.Xml.XmlDocument.LoadXml(String xml)
Code and example data:
var string ="";
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(result);
return xmldoc;
var string ="<Wssrecords PageSize="1" PageCount="1" PageNumber="1" TotalRecordsFetched="1" ><Wss RowNumber ="0" ID ="533" DocumentName ="FXFrameworknkolla" UserId ="kpease" ActualName ="Narasimha" TimeLastModified ="2014-08-13 12:21:25" ModifiedBy ="AGILITYINDIA\fxservice" CheckOutStatus ="None" ServerRelativeUrl ="/TestSite/NASeForms/FXFrameworknkolla.png" DocUrl ="http://inhydfrmtwo:33575/TestSite/NASeForms/FXFrameworknkolla.png" selected ="" FileSize ="1971" DocType ="General" _CheckinComment = "533;#" _EditMenuTableStart = "FXFrameworknkolla.png" _EditMenuTableEnd = "533" LinkFilenameNoMenu = "FXFrameworknkolla.png" LinkFilename = "FXFrameworknkolla.png" DocIcon = "png" ServerUrl = "/TestSite/NASeForms/FXFrameworknkolla.png" EncodedAbsUrl = "http://inhydfrmtwo:33575/TestSite/NASeForms/FXFrameworknkolla.png" BaseName = "FXFrameworknkolla" FileSizeDisplay = "1971" MetaInfo = "533;#ActualName:SW|Narasimha
vti_rtag:SW|rt:6815B6B4-1BB2-49D5-8326-3FFCE74A5E1A@00000000007
vti_etag:SW|"{6815B6B4-1BB2-49D5-8326-3FFCE74A5E1A},7"
vti_parserversion:SR|12.0.0.6665
DocType:SW|General
vti_modifiedby:SR|AGILITYINDIA\\fxservice
vti_docstoreversion:IR|7
ContentTypeId:SW|0x0101000069C1CDA7F5314AB877D5A06E6948E3
vti_lastheight:IX|48
UserId:SW|kpease
vti_author:SR|AGILITYINDIA\\fxservice
FormCode:SW|EXP-7204
vti_lastwidth:IX|48
vti_replid:SR|rid:{6815B6B4-1BB2-49D5-8326-3FFCE74A5E1A}
" _Level = "1" _IsCurrentVersion = "1" SelectTitle = "533" SelectFilename = "533" owshiddenversion = "8" _UIVersion = "512" _UIVersionString = "1.0" Order = "53300.0000000000" GUID = "{763F4C63-9271-4CF6-9CCF-2C419A0AE698}" WorkflowVersion = "1" ParentVersionString = "533;#" ParentLeafName = "533;#" Combine = "0" RepairDocument = "0"/></Wssrecords>"
Does anyone know how I can solve this xml problem? Please give me some suggestions.
Upvotes: 0
Views: 323
Reputation: 49260
That string simply isn't valid (well-formed) XML. You cannot "convert" an arbitrary string to XML - at least not without some effort.
In your case, the string content looks like XML, but has errors.
In particular, look at this "line":
vti_etag:SW|"{6815B6B4-1BB2-49D5-8326-3FFCE74A5E1A},7"
That is actually part of "MetaInfo" attribute's value. Since it is part of an attribute, it cannot contain literal quotes. For your example, turning it into
vti_etag:SW|"{6815B6B4-1BB2-49D5-8326-3FFCE74A5E1A},7"
Would result in valid XML.
I suggest you read up on XML in general, and about escaping in particular.
Also, if you have control over the generation of your XML data, make sure you use the proper tools to generate it in the first place. Don't use string.Format
, StringBuilder
or such, but rather XmlWriter
, XDocument
, XmlDocument
, etc.
Upvotes: 4
Reputation: 3653
Go here to check your XML.
The first error would be the part where it says:
vti_etag:SW|"{6815B6B4-1BB2-49D5-8326-3FFCE74A5E1A},7"
since that would not mix well with your other attribute values that are quoted as well.
If you truly don't have control over the string response, you'd best perform manipulation on the string to fix it before you load it to XmlDocument.
Upvotes: 0