使用混合类型反序列化xml文件

时间:2019-02-08 11:34:36

标签: c# .net xml deserialization

非常感谢您在.NET中创建类和反序列化xml文件方面的任何帮助

xml数据示例

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root>
    <Article>
        <Story>
            <Title>Some Title 1</Title>
            <Author>John</Author>
            <Lead>Some Lead 1</Lead>
            <Subtitle>SubTitle 1-1</Subtitle>
            <Body>body 1-1-1</Body>
            <Body>body 1-1-2</Body>
            <Body>body 1-1-3</Body>
            <Body>body 1-1-4</Body>
            <Subtitle>SubTitle 1-2</Subtitle>
            <Body>body 1-2-1</Body>
            <Body>body 1-2-2</Body>
            <Subtitle>SubTitle 1-3</Subtitle>
            <Body>body 1-3-1</Body>
            <Body>body 1-3-2</Body>
            <Body>body 1-3-3</Body>
        </Story>
        <Story>
            <Title>Some Title 2</Title>
            <Author>Adam</Author>
            <Lead>Some Lead 2</Lead>
            <Subtitle>SubTitle 2-1</Subtitle>
            <Body>body 2-1-1</Body>
            <Body>body 2-1-2</Body>
            <Body>body 2-1-3</Body>
            <Subtitle>SubTitle 2-2</Subtitle>
            <Body>body 2-2-1</Body>
            <Body>body 2-2-2</Body>
            <Subtitle>SubTitle 2-3</Subtitle>
            <Body>body 2-3-1</Body>
        </Story>
        <Story>
            <Picture>
                <Image href="someFile1.jpg"></Image>
                <Credit>Credit 1</Credit>
                <Description>Description Image 1</Description>
            </Picture>
        </Story>
        <Story>
            <Picture>
                <Image href="someFile2.jpg"></Image>
                <Credit>Credit 2</Credit>
            </Picture>
        </Story>
    </Article>
</Root>

我已经为此准备了Domain类(但是对于我的xml文件而言,也许不是最好的主意)

using System.Collections.Generic;
using System.Xml.Serialization;

namespace QgeImagingXmlConnector.Domain
{
    [XmlRoot(ElementName = "Root")]
    public class InputXmlModel
    {
        [XmlElement("Article")]
        public List<Article> Articles { get; set; }
    }


    public class Article
    {
        [XmlElement("Story")]
        public List<Story> Stories { get; set; }
    }

    public class Story
    {
        public string Title { get; set; }
        public string Author { get; set; }
        public string Lead { get; set; }
        public List<Item> Items { get; set; }
        //OR
        public List<StoryPicture> Pictures { get; set; }
    }

    public class StoryPicture
    {
        public string ImageHref { get; set; }
        public string Credit { get; set; }
        public string Description { get; set; }
    }


    public class Item
    {
        public string ItemType { get; set; } //   Possible: Body or Subtitle
        public string ItemText { get; set; }
    }
}

反序列化的方法和方法

public InputXmlModel GetInputXmlModelByXmlFile(string filePath)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(InputXmlModel));
            TextReader tr = new StreamReader(filePath);
            var result = (InputXmlModel)serializer.Deserialize(tr);
            tr.Close();
            return result;
        }

我的问题是:如何更改类的工作方式(通过添加一些属性或更改结构)

故事可以通过带有某些内容的故事或仅具有图片的故事来实现 所以在我的课上我增加了2个课Story和StoryPicture 在故事内容中,我们可以有很多正文或字幕标签-但是顺序很重要

致谢

P.S。

这就是我想要得到的结果 我只为第一个故事放数据

var result = new InputXmlModel
            {
                Articles = new List<Article>
                {
                    {
                        new Article
                        {
                            Stories = new List<Story>
                            {
                                {new Story
                                    {
                                        Title = "Some Title 1",
                                        Author = "John",
                                        Lead="Some Lead 1",
                                        Items = new List<Item>
                                        {
                                            new Item{ItemType = "Subtitle", ItemText = "SubTitle 1-1"},
                                            new Item{ItemType = "Body", ItemText = "body 1-1-1"},
                                            new Item{ItemType = "Body", ItemText = "body 1-1-2"},
                                            new Item{ItemType = "Body", ItemText = "body 1-1-3"},
                                            new Item{ItemType = "Body", ItemText = "body 1-1-4"},
                                            new Item{ItemType = "Subtitle", ItemText = "SubTitle 1-2"},
                                            new Item{ItemType = "Body", ItemText = "body 1-2-1"},
                                            new Item{ItemType = "Body", ItemText = "body 1-2-2"},
                                            new Item{ItemType = "Subtitle", ItemText = "SubTitle 1-3"},
                                            new Item{ItemType = "Body", ItemText = "body 1-3-1"},
                                            new Item{ItemType = "Body", ItemText = "body 1-3-2"},
                                            new Item{ItemType = "Body", ItemText = "body 1-3-3"},
                                        }
                                    } 
                                }
                                // here next 3 stories ( one with Items, two for pictures )
                            }
                        }
                    }
                }
            };

3 个答案:

答案 0 :(得分:1)

如果您在XML中取出int remove(Item item) { if(this.first == null) return 0; if(this.first == item) { // remove root item Node node = this.first.next; this.first.next = null; this.first = node; return 1; } Node prv = this.first; while (prv.next != item) prv = prv.next; // item was not found if(prv == null) return 0; Node node = prv.next.next; prv.next.next = null; prv.next = node; return 1; } 标签,然后将反序列化的方法更改为:

<Root>

应该可以。

答案 1 :(得分:1)

是的,您也可以这样做。 您的模型课要:

using System.Collections.Generic;
using System.Xml.Serialization;

namespace QgeImagingXmlConnector.Domain
{
    [XmlRoot(ElementName = "Root")]
    public class InputXmlModel
    {
        [XmlElement("Article")]
        public List<Article> Articles { get; set; }
    }


    public class Article
    {
        [XmlElement("Story")]
        public List<Story> Stories { get; set; }
    }

    public class Story
    {
        public string Title { get; set; }
        public string Author { get; set; }
        public string Lead { get; set; }
        [XmlElement("Item")]
        public List<Item> Items { get; set; }
        [XmlElement("Picture")]
        public List<StoryPicture> Pictures { get; set; }
    }

    public class StoryPicture
    {
        public string ImageHref { get; set; }
        public string Credit { get; set; }
        public string Description { get; set; }
    }


    public class Item
    {
        public string ItemType { get; set; } //   Possible: Body or Subtitle
        public string ItemText { get; set; }
    }
}

您的XML如下:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root>
    <Article>
        <Story>
            <Title>Some Title 1</Title>
            <Author>John</Author>
            <Lead>Some Lead 1</Lead>
            <Subtitle>SubTitle 1-1</Subtitle>
            <Body>body 1-1-1</Body>
            <Body>body 1-1-2</Body>
            <Body>body 1-1-3</Body>
            <Body>body 1-1-4</Body>
            <Subtitle>SubTitle 1-2</Subtitle>
            <Body>body 1-2-1</Body>
            <Body>body 1-2-2</Body>
            <Subtitle>SubTitle 1-3</Subtitle>
            <Body>body 1-3-1</Body>
            <Body>body 1-3-2</Body>
            <Body>body 1-3-3</Body>
        </Story>
        <Story>
            <Title>Some Title 2</Title>
            <Author>Adam</Author>
            <Lead>Some Lead 2</Lead>
            <Subtitle>SubTitle 2-1</Subtitle>
            <Body>body 2-1-1</Body>
            <Body>body 2-1-2</Body>
            <Body>body 2-1-3</Body>
            <Subtitle>SubTitle 2-2</Subtitle>
            <Body>body 2-2-1</Body>
            <Body>body 2-2-2</Body>
            <Subtitle>SubTitle 2-3</Subtitle>
            <Body>body 2-3-1</Body>
             <Picture>
                <Image href="someFile1.jpg"></Image>
                <Credit>Credit 1</Credit>
                <Description>Description Image 1</Description>
            </Picture>
            <Picture>
                <Image href="someFile2.jpg"></Image>
                <Credit>Credit 2</Credit>
            </Picture>
        </Story>
    </Article>
</Root>

这是您要找的吗?

答案 2 :(得分:1)

尝试按照xml linq进行操作:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;


namespace ConsoleApplication100
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            Article article = doc.Descendants("Article").Select(x => new Article() { Stories = x.Elements("Story").Select(y => Story.ParseStory(y)).ToList() }).FirstOrDefault();

        }


    }
    public class InputXmlModel
    {
       public List<Article> Articles { get; set; }

    }


    public class Article
    {
        public List<Story> Stories { get; set; }
    }

    public class Story
    {
        public string Title { get; set; }
        public string Author { get; set; }
        public string Lead { get; set; }
        public List<Item> Items { get; set; }
        public List<StoryPicture> Pictures { get; set; }

        enum State
        {
            DEFAULT,
            SUBTITLE,

        }
        public static Story ParseStory(XElement xStory)
        {
            Story story = new Story();
            State state = State.DEFAULT;
            Item newItem = null;
            StoryPicture newPicture = null;
            foreach (XElement child in xStory.Elements())
            {
                switch(state)
                {
                    case State.DEFAULT :
                        switch (child.Name.LocalName)
                        {
                            case "Title" :
                                story.Title = (string)child;
                                break;
                            case "Author":
                                story.Author = (string)child;
                                break;
                            case "Lead":
                                story.Lead = (string)child;
                                break;
                            case "Subtitle":
                                newItem = new Item();
                                if (story.Items == null) story.Items = new List<Item>();
                                story.Items.Add(newItem);

                                state = State.SUBTITLE;
                                break;
                            case "Picture":
                                newPicture = new StoryPicture()
                                {
                                    ImageHref = (string)child.Element("Image").Attribute("href"),
                                    Credit = (string)child.Element("Credit"),
                                    Description = (string)child.Element("Description")
                                };
                                if (story.Pictures == null) story.Pictures = new List<StoryPicture>();
                                story.Pictures.Add(newPicture);

                                break;
                            default:
                                Console.WriteLine("Error");
                                Console.ReadLine();
                                break;
                        }
                        break;


                    case State.SUBTITLE :
                        switch (child.Name.LocalName)
                        {
                            case "Body" :
                                newItem.ItemType = "SubTitle";
                                newItem.ItemText = (string)child;
                                break;

                            case "Subtitle":
                                newItem = new Item();
                                if (story.Items == null) story.Items = new List<Item>();
                                story.Items.Add(newItem);
                                break;

                            default:
                                Console.WriteLine("Error");
                                Console.ReadLine();
                                break;
                        }

                        break;
                }
            }

            return story;
        }
    }

    public class StoryPicture
    {
        public string ImageHref { get; set; }
        public string Credit { get; set; }
        public string Description { get; set; }
    }
    public class Item
    {
        public string ItemType { get; set; } //   Possible: Body or Subtitle
        public string ItemText { get; set; }
    }



}