问题解析XML字符串的XDocument
我从我的网络API的控制器的构造如图接收XML字符串问题解析XML字符串的XDocument
- <JobXML xmlns="http://host.xxx.com">
- <JobsXML>
<ID>1</ID>
<Name>Dave</Name>
<Age>23</Age>
<JobTitle>Developer</JobTitle>
<StartDate>10/24/2013 6:40:28 AM</StartDate>
</JobsXML>
- <JobsXML>
<ID>2</ID>
<Name>John</Name>
<Age>44</Age>
<JobTitle>QA</JobTitle>
<StartDate>10/24/2013 6:40:28 AM</StartDate>
</JobsXML>
- <JobsXML>
<ID>3</ID>
<Name>Dave</Name>
<Age>23</Age>
<JobTitle>Senior Developer</JobTitle>
<StartDate>10/24/2013 6:40:28 AM</StartDate>
</JobsXML>
</JobXML>
当我再回到这个作为一个字符串,并尝试分析它回到XDOC一个如图所示:
private static string HandleResponse(HttpWebResponse httpResponse)
{
using (var responseReader = new StreamReader(httpResponse.GetResponseStream(), Encoding.UTF8))
{
string responsePayload = responseReader.ReadToEnd();
var newxDoc = XDocument.Parse(responsePayload);
return responsePayload;
}
}
字符串“RES ponsePayLoad”在运行时被设定为示出:
"<JobXML xmlns=\"http://host.adp.com\"><JobsXML><ID>1</ID><Name>Dave</Name><Age>23</Age><JobTitle>Developer</JobTitle><StartDate>10/24/2013 6:45:22 AM</StartDate></JobsXML><JobsXML><ID>2</ID><Name>John</Name><Age>44</Age><JobTitle>QA</JobTitle><StartDate>10/24/2013 6:45:22 AM</StartDate></JobsXML><JobsXML><ID>3</ID><Name>Dave</Name><Age>23</Age><JobTitle>Senior Developer</JobTitle><StartDate>10/24/2013 6:45:22 AM</StartDate></JobsXML></JobXML>"
这是给我在异常‘的newxDoc’对象:
XmlException是未处理的。根级别的数据无效。 1号线,位置1.
谁能告诉我我哪里出错了吗?
问题是您的responsePayLoad字符串不是有效的XML。
你的问题是在这里:
"<JobXML xmlns=\"http://host.adp.com\">
引号字符的开始和结束的字符串,以及引号前反斜杠的存在,是导致XML是格式不正确。如果字符串没有这些报价在开始和结束,反斜线的XML将是有效的,即这将使合式的XML:
<JobXML xmlns="http://host.adp.com">...<\JobXML>
至于为什么会发生此问题,则可能是因为您创建XDocument的方式。微软的文档here中的例子,表明其采用了特定XDeclaration一个XDocument应该使用下面的构造函数实例化:
XDocument(XDeclaration, Object[])
所以我想尝试重新分解代码,以使我们这个方法如
private string CreateXDoc(IEnumerable<PassedJSONConverted> passed)
{
XNamespace xmlns = "http://host.adp.com";
var xdec = new XDeclaration("1.0", "utf-8", "yes");
var jobListElement = new XElement(xmlns + "JobXML");
foreach (var objectItem in passed)
{
var jobXml = new XElement(xmlns + "JobsXML",
new XElement(xmlns + "ID", objectItem.ID.ToString()),
new XElement(xmlns + "Name", objectItem.Name),
new XElement(xmlns + "Age", objectItem.Age.ToString()),
new XElement(xmlns + "JobTitle", objectItem.JobTitle),
new XElement(xmlns + "StartDate", objectItem.StartDate));
jobListElement.Add(jobXml);
}
var doc = new XDocument(
xdec,
new XElement(jobListElement)
);
//Format without new lines
return doc.ToString(SaveOptions.DisableFormatting);
}
如果这不也是工作,请尝试关闭禁用格式化的CreateXDoc即
return doc.ToString();
尝试增加< ? XML版本= “1.0” 编码= “UTF-8” ? >到你的XML字符串的开头。
不要担心转义引号,它们在那里,因为你的XML包装在一个字符串中。
上述两个答案相结合,将它当我重复XML脚本我得到这个错误
XML解析错误理清
:没有很好地形成 位置:无题 - 1.XML 行号2,第15列: http://host.adp。1Dave23 --------------^ 开发者10/24/2013 6:45:22 AM2John44QA10/24/2013 6:45:22 AM3Dave23高级开发者10/24/2013 6: 45:22 AM
这是正确的形式
<?xml version="1.0" encoding="utf-8"?>
<JobXML xmlns="http://host.adp.com\"><JobsXML><ID>1</ID><Name>Dave</Name><Age>23</Age><JobTitle>Developer</JobTitle><StartDate>10/24/2013 6:45:22 AM</StartDate></JobsXML><JobsXML><ID>2</ID><Name>John</Name><Age>44</Age><JobTitle>QA</JobTitle><StartDate>10/24/2013 6:45:22 AM</StartDate></JobsXML><JobsXML><ID>3</ID><Name>Dave</Name><Age>23</Age><JobTitle>Senior Developer</JobTitle><StartDate>10/24/2013 6:45:22 AM</StartDate></JobsXML></JobXML>
为你问,解析XML字符串的XDocument:
只是试试这个代码
XDocument document = XDocument.Parse(xml);
,但我有一个问题如果这个好的话r巨大的xml字符串,比如带有多于一个hunderd千行代码的xml。 我试过这个方法为一个xml文件与10000行代码,它花了1.5秒
我已经试过'XDocument.Parse()''responsePayLoad' XML并没有错误。 –
我有一个类似的问题,并发现它是编码相关的。在StreamReader中将'Encoding'设置为'Default'对我有用。 –