3

I am receiving an XML string from the controller of my web API that is constructed as shown:

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

This is fine and the XML is set as shown:

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

When I then return this as a string and try to parse it back to an xDoc as shown:

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

The string 'responsePayLoad' at runtime is set as shown:

 "<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>"

This is giving me an exception at the 'newxDoc' object of:

XmlException is unhandled. Data at the root level is invalid. Line 1, position 1.

Can anyone tell me where I'm going wrong?

4

4 回答 4

4

问题是您的 responsePayLoad 字符串不是有效的 XML。

你的问题在这里:

"<JobXML xmlns=\"http://host.adp.com\">

字符串开头和结尾处存在引号字符以及引号之前的反斜杠会导致 XML 格式错误。如果您的字符串在开头和结尾没有这些引号以及反斜杠,则 XML 将是有效的,即这将使 XML 格式正确:

<JobXML xmlns="http://host.adp.com">...<\JobXML>

至于为什么会出现这个问题,可能是因为您创建 XDocument 的方式。此处Microsoft 文档中的示例表明,使用特定 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();
于 2013-10-28T00:34:25.697 回答
1

尝试将 <?xml version="1.0" encoding="UTF-8" ?> 添加到 XML 字符串的开头。

不用担心转义引号,它们在那里是因为您的 XML 被包裹在一个字符串中。

于 2013-10-29T05:57:41.517 回答
0

如您所问,将 XML 字符串解析为 xdocument:

只需尝试此代码

 XDocument document = XDocument.Parse(xml);

但是我担心这是否适用于巨大的 xml 字符串,例如具有超过十万行代码的 xml。我已经为一个 10000 行代码的 xml 文档尝试了这种方法,它花了 1.5 秒

于 2013-11-02T00:18:27.517 回答
0

以上两个答案结合起来就可以解决了

当我重复 xml 脚本时,我得到了这个错误

XML 解析错误:格式不正确 位置:Untitled-1.xml 第 2 行,第 15 列:http://host.adp.com\">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>
于 2013-11-01T13:52:17.733 回答