简体   繁体   English

解析XML数据(2,2)

[英]Parsing XML Data (2,2)

I've got a unique problem when I attempt to deserialize XML. 当我尝试反序列化XML时,我遇到了一个独特的问题。

The method is as follows: 方法如下:

XmlSerializer serialize = new XmlSerializer(typeof(APIFile));
using(FileStream stream = new FileStream(file, FileMode.Open))
     using(XmlReader reader= XmlReader.Create(stream))
     {
          APIFile model = serialize.Deserialize(reader) as APIFile;
          Console.WriteLine(model.lSubmission.Select(m => m.Id));
     }

So when the code hits the model I receive an error stating: 因此,当代码到达model我会收到一条错误消息:

  • xmlns="> Was not expected.
  • There is an error in XML document (2,2).

The model built was populated through Visual Studio Edit -- Past Special -- XML Class . 通过Visual Studio 编辑-过去的特殊-XML类填充了构建的模型。

I've tampered with the following line: 我篡改了以下行:

[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]

I've modified the line: 我修改了这一行:

  • Namespace 命名空间
  • xmlns xmlns
  • Removed the line. 删除行。
  • Changed from a Root Attribute 从根属性更改

But the error persist and I'm not entirely sure why. 但是错误仍然存​​在,我不确定为什么。


<?xml version="1.0" encoding="UTF-8"?>
<CanvasResult>
     <TotalPages>8</TotalPages>
     <CurrentPage>1</CurrentPage>
     <Submissions>
          <Submission Id="5840790">
               <Form Id="718013">
                    <Name>Proof of Delivery</Name>
                    <Status>retired</Status>
                    <Version>6</Version>
               </Form>

That is the first little bit of the XML, after that is some more minor content / section which will build out a little further. 那是XML的第一部分,其后是一些较小的内容/部分,将进一步扩展。 My document doesn't have a xmlns anywhere in the file, is it possible the Reader or XmlSerializer are adding it? 我的文档在文件的任何位置都没有xmlns,是否有可能是Reader或XmlSerializer添加了它?

I managed to isolate the issue to a couple of items: 我设法将问题归结为两个方面:

  • The data model wasn't handling all of the content that was inserted. 数据模型无法处理所有插入的内容。
  • The third party API was passing & in the XML, which also caused an error. 第三方API正在XML中传递& ,这也导致了错误。

I wrote a simple validation method that ran through the file to validate the XML and fix any malformed tags or symbols. 我编写了一种简单的验证方法,该方法遍历文件以验证XML并修复任何格式错误的标记或符号。

The code that worked that finally deserialized the values is: 最终反序列化值的代码是:

    public static void ParseToModel(string file)
    {
        if (string.IsNullOrEmpty(file))
            throw new ArgumentNullException();

        XmlSerializer serialize = new XmlSerializer(typeof(CanvasResult));
        using(FileStream stream = new FileStream(file, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
            using(XmlReader reader= XmlReader.Create(stream))
            {
                CanvasResult model = serialize.Deserialize(reader) as CanvasResult;
                ParseToDatabase(model);
            }
    }

The method is called, which will take the file parameter then correlate the content to the data model. 调用该方法,该方法将使用file参数,然后将内容与数据模型相关联。 At the end it calls a separate method called ParseToDatabase which breaks the content down and placed into the proper column within the database. 最后,它调用一个名为ParseToDatabase的单独方法,该方法将内容分解并放入数据库的适当列中。

Easiest way i found of dealing with xml is to use XDocuments / XElements ... 我发现处理xml的最简单方法是使用XDocuments / XElements ...

using(var reader = StreamReader(new FileStream(file, FileMode.Open)))
{
   var data = reader.ReadToEnd();
   var xml = new XElement(data);
   //TODO: query xml with linq or whatever
}

Works with whole docs and fragments 适用于整个文档和片段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM