deserialize existing xml into custom object

落爺英雄遲暮 提交于 2019-12-12 22:34:30

问题


I have the following xml:

<state>
<groups>
    <group id='1' name='Basic Search Options'>
        <control name='Building' label='In' display='true' configurable='false'/>
        <control name='SearchType' label='For' display='true' configurable='false'/>
        <control id='1' default='C' name='Search By' label='By'>
            <option searchtype='C' searchmode='Cnumber' value='CNumber' label='C Number' display='true'/>
            <option searchtype='C' searchmode='crossrefnumber' value='CNumber1' label='Cross Reference Number' display='true'/>
            <option searchtype='P' searchmode=''  value='CaseNumber' label='P Name' display='true'/>
            <option searchtype='P' searchmode='' value='CaseNumber' label='A Name' display='false'/>
        </control>
    </group>
    <group id='2' name='Advanced Search Options'>
        <control name='Ctatus' label='C Status' display='true'/>
        <control name='DateFiled' label='Date Filed' display='true'/>
    </group>
</groups>

How would I de-serialize this into the following object? I dont want my xml to have the following tags "ArrayofGroup", instead the xml should have custom tags like mention above.

public class GroupOfControls
{
public int instanceId { get; set; }
public int GroupId { get; set; }
public string Name { get; set; }
public List<SearchControl> Group { get; set; }
}

public class SearchControl
{
public string Name { get; set; }
public string Label { get; set; }
public bool Display { get; set; }
public string Default { get; set; }
public List<SearchOption> SearchOptions { get; set; }
}

public class SearchOption
{
public string Value { get; set; }
public string Label { get; set; }
public bool Display { get; set; }
public string SearchMode { get; set; }
public string SearchType { get; set; }
}
}

回答1:


If you don't have an XSD file, you need to create one from your XML. You can do this with the Visual Studio Command Line using the following command:

xsd myfilename.xml

Once you have an XSD file this should be easy enough.

I am working with Visual Studio 2010 (C#/.Net 4) and I would do this:

First I would make a new solution in Visual Studio:

Then you need to import the XSD file into your project by right clicking on your solution, and selecting Add => Existing Item and browsing to the XSD.

Once you have the XSD inside your project you need to launch the Visual Studio command prompt, use cd to navigate to your projects directory and then type the following command:

xsd myFilename.xsd /classes

This generates the C# class you are going to deserialize your XML into. Use the Add Existing Item dialogue to import this new class into your solution.

Next you add using System.Xml.Serialization; and using System.IO; to your using statements. Then use the following code to deserialize your XML to an object (assuming the XML validates against your XSD). The most recent XSD I could file I called ResponseData as shown below:

With this in mind, my C# code is below:

using System;
using System.IO;
using System.Xml.Serialization;

namespace XML_Deserialization
{
    class Program
    {
        static void Main(string[] args)
        {
            ResponseData myResponseData = new ResponseData();

            XmlSerializer mySerializer = new XmlSerializer(typeof(ResponseData));
            StreamReader myStreamReader = new StreamReader(@"C:\Users\JMK\Documents\Visual Studio 2010\Projects\scratch\XML Deserialization\XML Deserialization\text.xml");
            myResponseData = (ResponseData)mySerializer.Deserialize(myStreamReader);

            Console.ReadLine();
        }
    }
}


来源:https://stackoverflow.com/questions/11398315/deserialize-existing-xml-into-custom-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!