将XML字符串解析为xDocument存在问题。

3

我从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"));

        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'对象处抛出XmlException异常:

未处理的XmlException。根级别上的数据无效。第一行,第一位置。

有人能告诉我哪里出错了吗?


1
我已经尝试在那个responsePayLoad XML上使用XDocument.Parse(),并且没有出现错误。 - Chuck Savage
我曾经遇到过类似的问题,后来发现是编码相关的。在 StreamReader 中将 Encoding 设置为 Default 对我有用。 - Andrei V
4个回答

4
问题在于您的responsePayLoad字符串不是有效的XML。
您的问题在这里:
"<JobXML xmlns=\"http://host.adp.com\">

在字符串的开头和结尾有引号字符,以及引号前面的反斜杠会导致XML格式不正确。如果您的字符串没有这些引号和反斜杠,则XML将是有效的,即XML将变为格式良好:

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

关于为什么会出现这个问题,可能是因为你创建XDocument的方式不对。微软文档这里的示例表明,使用特定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();

1
尝试在XML字符串开头添加“<?xml version="1.0" encoding="UTF-8" ?>”。
不要担心转义引号,因为您的XML被包装在字符串中。

0

结合以上两个答案将解决它

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

XML解析错误:未格式化 位置:Untitled-1.xml 行号2,列15: http://host.adp.com\">1Dave23 --------------^ Developer10/24/2013 6:45:22 AM2John44QA10/24/2013 6:45:22 AM3Dave23Senior Developer10/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>

0
根据您的要求,将XML字符串解析为XDocument:
只需尝试这段代码。
 XDocument document = XDocument.Parse(xml);

但我有一个疑虑,对于像一个有超过十万行代码的大型xml字符串是否适用。我已经尝试了这种方法来处理一个有1万行代码的xml文档,需要1.5秒钟。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接