C# 将XML字符串解析为xDocument时出现问题
我正在从我的web API的控制器接收一个XML字符串,其构造如下所示:C# 将XML字符串解析为xDocument时出现问题,c#,xml,api,parsing,xmlexception,C#,Xml,Api,Parsing,Xmlexception,我正在从我的web API的控制器接收一个XML字符串,其构造如下所示: private string CreateXDoc(IEnumerable<PassedJSONConverted> passed) { XNamespace xmlns = "http://host.adp.com"; var doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
private string CreateXDoc(IEnumerable<PassedJSONConverted> passed)
{
XNamespace xmlns = "http://host.adp.com";
var doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
var jobListElement = new XElement(xmlns + "JobXML");
foreach (var objectItem in passed)
{
var loopElement = 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(loopElement);
}
doc.Add(jobListElement);
//Format without \n's
return doc.ToString(SaveOptions.DisableFormatting);
}
- <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>
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;
}
}
"<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>"
运行时设置字符串“responsePayLoad”,如下所示:
private string CreateXDoc(IEnumerable<PassedJSONConverted> passed)
{
XNamespace xmlns = "http://host.adp.com";
var doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
var jobListElement = new XElement(xmlns + "JobXML");
foreach (var objectItem in passed)
{
var loopElement = 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(loopElement);
}
doc.Add(jobListElement);
//Format without \n's
return doc.ToString(SaveOptions.DisableFormatting);
}
- <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>
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;
}
}
"<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>"
“1Dave23Developer10/24/2013 6:45:22 AM2John44QA10/24/2013 6:45:22 am3Dave23高级开发人员10/24/2013 6:45:22 AM”
这给了我一个“newxDoc”对象的异常:
XmlException未处理。根级别的数据无效。第1行,位置1
谁能告诉我哪里出错了吗?问题是您的responsePayLoad字符串不是有效的XML 你的问题是:
"<JobXML xmlns=\"http://host.adp.com\">
所以我会尝试重新分解你的代码,让我们了解这个方法
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);
}
尝试添加到XML字符串的开头
不要担心转义引号,它们在那里是因为XML被包装在一个字符串中。以上两个答案组合在一起可以解决问题 当我重复xml脚本时,我得到了这个错误 XML分析错误:格式不正确 位置:untitle-1.xml 第2行第15列: http://host.adp.com\“>1行程23 --------------^ Developer10/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>
1 2013年12月10日上午6:45:22至2013年12月24日上午6:45:22至2013年12月24日上午6:45:22至2013年12月24日上午6:45:22至23日高级开发人员
如您所问,将XML字符串解析为xdocument:
只需尝试一下这段代码
XDocument document = XDocument.Parse(xml);
但我有一个担心,这是否适用于庞大的xml字符串,例如包含超过十万行代码的xml。
我对一个包含10000行代码的xml文档尝试了这种方法,花了1.5秒我尝试了
XDocument.Parse()
在responsePayLoad
XML上,没有错误。我遇到了类似的问题,并发现它与编码有关。在StreamReader
中将编码设置为默认值对我有效。