gpt4 book ai didi

c# - 将解析 XML 字符串发送到 xDocument

转载 作者:数据小太阳 更新时间:2023-10-29 02:24:05 24 4
gpt4 key购买 nike

我从我的 Web API 的 Controller 接收到一个 XML 字符串,其构造如下所示:

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);
}

这很好,XML 设置如下所示:

- <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;
}
}

字符串 '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>"

这在“newxDoc”对象处给了我一个异常(exception):

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();

关于c# - 将解析 XML 字符串发送到 xDocument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19563822/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com