将XMLDocument读入对象

时间:2011-05-15 14:58:58

标签: c# xml

这应该很简单,但我很难理解如何使用XMLReader或Linq来做到这一点。

这是我周末工作的宠物项目,以提高我的技能 - 理想情况下,这应该将所有嵌套对象作为头等对象,但我对如何解释这个的想法持开放态度

编辑:我已经尝试过XMLReader,但是我的代码错误超过了30行并且看起来很糟糕 - 在C#中如何将XML转换为对象?

我可以使用反序列化器将其反序列化为对象吗?如果是这样,我是否可以手工构建所有对象?

    '<level4>
        <module>
            <moduleCode>ECSC401</moduleCode>
            <moduleTitle>Programming Methodology</moduleTitle>
            <credits>15</credits>
            <semester>1</semester>
            <assessmentDetails>
                <assessment>
                    <assessmentName>Test1</assessmentName>
                    <assessmentType>Coursework</assessmentType>
                    <assessmentWeighting>30</assessmentWeighting>
                    <assessmentDueDate></assessmentDueDate>
                </assessment>
                <assessment>
                    <assessmentName>Coursework</assessmentName>
                    <assessmentType>Coursework</assessmentType>
                    <assessmentWeighting>40</assessmentWeighting>
                    <assessmentDueDate></assessmentDueDate>
                </assessment>
                <assessment>
                    <assessmentName>Test2</assessmentName>
                    <assessmentType>Coursework</assessmentType>
                    <assessmentWeighting>30</assessmentWeighting>
                    <assessmentDueDate></assessmentDueDate>
                </assessment>
            </assessmentDetails>
        </module>
        <module>
            <moduleCode>ECSC404</moduleCode>
            <moduleTitle>Computer Systems Fundamentals</moduleTitle>
            <credits>15</credits>
            <semester>1</semester>
            <assessmentDetails>
                <assessment>
                    <assessmentName>Test1</assessmentName>
                    <assessmentType>Coursework</assessmentType>
                    <assessmentWeighting>30</assessmentWeighting>
                    <assessmentDueDate></assessmentDueDate>
                </assessment>
                <assessment>
                    <assessmentName>Test2</assessmentName>
                    <assessmentType>Coursework</assessmentType>
                    <assessmentWeighting>30</assessmentWeighting>
                    <assessmentDueDate></assessmentDueDate>
                </assessment>
                <assessment>
                    <assessmentName>Test3</assessmentName>
                    <assessmentType>Coursework</assessmentType>
                    <assessmentWeighting>40</assessmentWeighting>
                    <assessmentDueDate></assessmentDueDate>
                </assessment>
            </assessmentDetails>
        </module>
        <module>
            <moduleCode>EBSY401</moduleCode>
            <moduleTitle>Information and Data Modelling</moduleTitle>
            <credits>15</credits>
            <semester>1</semester>
            <assessmentDetails>
                <assessment>
                    <assessmentName>Test</assessmentName>
                    <assessmentType>Coursework</assessmentType>
                    <assessmentWeighting>25</assessmentWeighting>
                    <assessmentDueDate></assessmentDueDate>
                </assessment>
                <assessment>
                    <assessmentName>Coursework1</assessmentName>
                    <assessmentType>Coursework</assessmentType>
                    <assessmentWeighting>10</assessmentWeighting>
                    <assessmentDueDate></assessmentDueDate>
                </assessment>
                <assessment>
                    <assessmentName>Coursework2</assessmentName>
                    <assessmentType>Coursework</assessmentType>
                    <assessmentWeighting>35</assessmentWeighting>
                    <assessmentDueDate></assessmentDueDate>
                </assessment>                    
                <assessment>
                    <assessmentName>Coursework3</assessmentName>
                    <assessmentType>Coursework</assessmentType>
                    <assessmentWeighting>30</assessmentWeighting>
                    <assessmentDueDate></assessmentDueDate>
                </assessment>
            </assessmentDetails>
        </module>
        <module>
            <moduleCode>ECSC405</moduleCode>
            <moduleTitle>Software Development Principles</moduleTitle>
            <credits>15</credits>
            <semester>2</semester>
            <assessmentDetails>
                <assessment>
                    <assessmentName>Test1</assessmentName>
                    <assessmentType>Coursework</assessmentType>
                    <assessmentWeighting>30</assessmentWeighting>
                    <assessmentDueDate></assessmentDueDate>
                </assessment>
                <assessment>
                    <assessmentName>Coursework</assessmentName>
                    <assessmentType>Coursework</assessmentType>
                    <assessmentWeighting>40</assessmentWeighting>
                    <assessmentDueDate></assessmentDueDate>
                </assessment>
                <assessment>
                    <assessmentName>Test2</assessmentName>
                    <assessmentType>Coursework</assessmentType>
                    <assessmentWeighting>30</assessmentWeighting>
                    <assessmentDueDate></assessmentDueDate>
                </assessment>
            </assessmentDetails>
        </module>
        <module>
            <moduleCode>ECSC407</moduleCode>
            <moduleTitle>Web Technology</moduleTitle>
            <credits>15</credits>
            <semester>2</semester>
            <assessmentDetails>
                <assessment>
                    <assessmentName>Tutorials</assessmentName>
                    <assessmentType>Coursework</assessmentType>
                    <assessmentWeighting>20</assessmentWeighting>
                    <assessmentDueDate></assessmentDueDate>
                </assessment>
                <assessment>
                    <assessmentName>Coursework</assessmentName>
                    <assessmentType>Coursework</assessmentType>
                    <assessmentWeighting>20</assessmentWeighting>
                    <assessmentDueDate></assessmentDueDate>
                </assessment>
                <assessment>
                    <assessmentName>Exam</assessmentName>
                    <assessmentType>Exam</assessmentType>
                    <assessmentWeighting>60</assessmentWeighting>
                    <assessmentDueDate></assessmentDueDate>
                </assessment>
            </assessmentDetails>
        </module>
        <module>
            <moduleCode>ECSC409</moduleCode>
            <moduleTitle>Software Engineering Principles</moduleTitle>
            <credits>15</credits>
            <semester>2</semester>
            <assessmentDetails>
                <assessment>
                    <assessmentName>Test1</assessmentName>
                    <assessmentType>Coursework</assessmentType>
                    <assessmentWeighting>40</assessmentWeighting>
                    <assessmentDueDate></assessmentDueDate>
                </assessment>
                <assessment>
                    <assessmentName>Coursework1</assessmentName>
                    <assessmentType>Coursework</assessmentType>
                    <assessmentWeighting>30</assessmentWeighting>
                    <assessmentDueDate></assessmentDueDate>
                </assessment>
                <assessment>
                    <assessmentName>Coursework2</assessmentName>
                    <assessmentType>Coursework</assessmentType>
                    <assessmentWeighting>30</assessmentWeighting>
                    <assessmentDueDate></assessmentDueDate>
                </assessment>
            </assessmentDetails>
        </module>
        <module>
            <moduleCode>ECSC408</moduleCode>
            <moduleTitle>Mathematics for Computing</moduleTitle>
            <credits>15</credits>
            <semester>0</semester>
            <assessmentDetails>
                <assessment>
                    <assessmentName>Coursework</assessmentName>
                    <assessmentType>Coursework</assessmentType>
                    <assessmentWeighting>50</assessmentWeighting>
                    <assessmentDueDate></assessmentDueDate>
                </assessment>
                <assessment>
                    <assessmentName>Exam</assessmentName>
                    <assessmentType>Exam</assessmentType>
                    <assessmentWeighting>50</assessmentWeighting>
                    <assessmentDueDate></assessmentDueDate>
                </assessment>
            </assessmentDetails>
        </module>
        <module>
            <moduleCode>EBSY400</moduleCode>
            <moduleTitle>Communication and Learning Skills</moduleTitle>
            <credits>15</credits>
            <semester>0</semester>
            <assessmentDetails>
                <assessment>
                    <assessmentName>Presentation</assessmentName>
                    <assessmentType>Coursework</assessmentType>
                    <assessmentWeighting>30</assessmentWeighting>
                    <assessmentDueDate>2010-05-06</assessmentDueDate>
                </assessment>
                <assessment>
                    <assessmentName>Portfolio</assessmentName>
                    <assessmentType>Coursework</assessmentType>
                    <assessmentWeighting>70</assessmentWeighting>
                    <assessmentDueDate>2010-05-06</assessmentDueDate>
                </assessment>
            </assessmentDetails>
        </module>
    </level4>

4 个答案:

答案 0 :(得分:1)

我想我以前不明白你的问题 因此,您需要创建一个XML序列化的类,并且XML将具有您显示的布局?

如果是这样,您需要:

  1. 创建XSD架构。 xsd.exe可以从xml推断模式。您可能需要手动更正推断的架构。推断架构:

    xsd.exe 1.xml
    

    这将生成1.xsd文件。

  2. 使用xsd.exe创建C#类。

    xsd.exe 1.xsd /classes /language:cs
    

    这将生成1.cs文件。

  3. 将1.cs文件添加到项目中,并在代码中使用自动生成的类。使用XmlSeralizer序列化/反序列化XML。

  4. 您可以在.NET SDK中找到xsd.exe(例如C:\ Program Files(x86)\ Microsoft SDKs \ Windows \ v7.0A \ Bin \ x64)。

答案 1 :(得分:0)

一种方法是使用.NET框架工具附带的XML Schema Definition Tool (Xsd.exe)。如果在Visual Studio中加载XML,则可以使用“创建架构”选项(XML&gt;创建架构),然后使用xsd.exe使用/c开关生成类。

xsd.exe /c myschema.xsd
  

“生成与之对应的类   指定的架构。读取XML数据   进入对象,使用   System.Xml.Serialization.XmlSerializer.Deserializer   方法“。

希望能给你一些想法吗?

答案 2 :(得分:0)

如果您想将XML文本读入XML对象,我建议使用LINQ-to-XML 您可以使用XDocument.ParseXDocument.Load方法 例如:

    var xmlText = @"
        <level4>
            <module>
                <moduleCode>ECSC401</moduleCode>
                <moduleTitle>Programming Methodology</moduleTitle>
                <credits>15</credits>
                <semester>1</semester>
                <assessmentDetails>
                    <assessment>
                        <assessmentName>Test1</assessmentName>
                        <assessmentType>Coursework</assessmentType>
                        <assessmentWeighting>30</assessmentWeighting>
                        <assessmentDueDate></assessmentDueDate>
                    </assessment>
                </assessmentDetails>
            </module>
        </level4>
        ";
    var xdocument = XDocument.Parse(xmlText);

答案 3 :(得分:0)

一种方法是将字符串与xml数据一起使用,另一种方法是“序列化程序”,XML序列化程序对象用于读取XML文档。

此函数返回对象,该对象是对象的初始化实例

public object LoadFromXmlDocument(string xmlDocument, XmlSerializer serializer)
{
    if (StringUtil.IsEmpty(xmlDocument))
       throw new ArgumentNullException("xmlDocument");
    else if (serializer == null)
       throw new ArgumentNullException("serializer");

//Initializing instance for textreader
TextReader reader = new StringReader(xmlDocument);

//Serializing a textreader content
object obj = serializer.Deserialize(reader);
reader.Close();
reader = null;
return obj;
}