Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在c#中将字符串转换为xml格式?_C#_Xml_Web Services - Fatal编程技术网

如何在c#中将字符串转换为xml格式?

如何在c#中将字符串转换为xml格式?,c#,xml,web-services,C#,Xml,Web Services,我正在处理c#web服务,这里我得到了字符串响应,我附上了下面的异常 异常消息: 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 r

我正在处理c#web服务,这里我得到了字符串响应,我附上了下面的异常

异常消息:

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)
代码和示例数据:

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>"
var string=”“;
XmlDocument xmldoc=新的XmlDocument();
LoadXml(结果);
返回xmldoc;
var string=“”
有人知道我如何解决这个xml问题吗?请给我一些建议。

检查您的XML

第一个错误是它说:

vti_etag:SW|"{6815B6B4-1BB2-49D5-8326-3FFCE74A5E1A},7"
因为它不能与引用的其他属性值很好地混合

如果您确实无法控制字符串响应,最好在将字符串加载到XmlDocument之前对其执行操作以修复它。

检查XML

第一个错误是它说:

vti_etag:SW|"{6815B6B4-1BB2-49D5-8326-3FFCE74A5E1A},7"
因为它不能与引用的其他属性值很好地混合


如果您确实无法控制字符串响应,最好在将字符串加载到XmlDocument之前对其执行操作以修复它。

该字符串根本无效(格式正确)。您无法将任意字符串“转换”为XML—至少需要一些努力

在您的例子中,字符串内容看起来像XML,但有错误

特别是,看看这条“线”:

这实际上是“MetaInfo”属性值的一部分。因为它是属性的一部分,所以不能包含文字引号。例如,将其转化为

vti_etag:SW|&quot;{6815B6B4-1BB2-49D5-8326-3FFCE74A5E1A},7&quot;
将生成有效的XML

我建议您通读XML,尤其是转义。
此外,如果您可以控制XML数据的生成,请确保首先使用适当的工具来生成它。不要使用
string.Format
StringBuilder
等,而是使用
XmlWriter
XDocument
XmlDocument
等。该字符串根本无效(格式正确)。您无法将任意字符串“转换”为XML—至少需要一些努力

在您的例子中,字符串内容看起来像XML,但有错误

特别是,看看这条“线”:

这实际上是“MetaInfo”属性值的一部分。因为它是属性的一部分,所以不能包含文字引号。例如,将其转化为

vti_etag:SW|&quot;{6815B6B4-1BB2-49D5-8326-3FFCE74A5E1A},7&quot;
将生成有效的XML

我建议您通读XML,尤其是转义。
此外,如果您可以控制XML数据的生成,请确保首先使用适当的工具来生成它。不要使用
string.Format
StringBuilder
等,而是使用
XmlWriter
XDocument
XmlDocument
等。我想如果没有XML,我们无法回答结果会是什么样子?它可能是JSON而不是XML吗?是的,{看起来像JSON,因为“{”字符看起来你得到的是json而不是xml。请在这里发布你的xml
var string=…
来吧,伙计。至少要努力让我们复制它。我认为没有xml我们无法回答结果是什么样子的?可能是json而不是xml吗?是的,{看起来像json是因为“{”字符看起来你得到的是json而不是xml。请在这里发布你的xml
var string=…
来吧,伙计。至少要努力让我们复制它。