English 中文(简体)
C# 中的 XML 序列数组
原标题:XML serialization array in C#
  • 时间:2012-05-24 18:13:49
  •  标签:
  • c#
  • .net
  • xml

我搞不清楚情况,我有一个Xml床单 看起来像这个

<root>
  <list id="1" title="One">
    <word>TEST1</word>
    <word>TEST2</word>
    <word>TEST3</word>
    <word>TEST4</word>
    <word>TEST5</word>
    <word>TEST6</word>   
  </list>
  <list id="2" title="Two">
    <word>TEST1</word>
    <word>TEST2</word>
    <word>TEST3</word>
    <word>TEST4</word>
    <word>TEST5</word>
    <word>TEST6</word>   
  </list>
</root>

我试图把它连成

public class Items
{
  [XmlAttribute("id")]
  public string ID { get; set; } 

  [XmlAttribute("title")]
  public string Title { get; set; }   

  //I don t know what to do for this
  [Xml... something]
  public list<string> Words { get; set; }   
}

//I don t this this is right either
[XmlRoot("root")]
public class Lists
{
  [XmlArray("list")]
  [XmlArrayItem("word")]
  public List<Items> Get { get; set; }
}

//Deserialize XML to Lists Class
using (Stream s = File.OpenRead("myfile.xml"))
{
   Lists myLists = (Lists) new XmlSerializer(typeof (Lists)).Deserialize(s);
}

我真的是新来的, 与XML和XML 串连, 任何帮助都会非常感激

最佳回答

如果你宣布你的班级为

public class Items
{
    [XmlAttribute("id")]
    public string ID { get; set; }

    [XmlAttribute("title")]
    public string Title { get; set; }

    [XmlElement("word")]
    public List<string> Words { get; set; }
}

[XmlRoot("root")]
public class Lists
{
    [XmlElement("list")]
    public List<Items> Get { get; set; }
}
问题回答

如果您只需要将 XML 读入一个对象结构, 使用 XLNQ 可能比较容易 。

给班级下这样的定义:

public class WordList
{
  public string ID { get; set; } 
  public string Title { get; set; }   
  public List<string> Words { get; set; }   
}

然后读读XML:

XDocument xDocument = XDocument.Load("myfile.xml");

List<WordList> wordLists =
(
    from listElement in xDocument.Root.Elements("list")
    select new WordList
    {
        ID = listElement.Attribute("id").Value,
        Title = listElement.Attribute("title").Value,
        Words = 
        (
            from wordElement in listElement.Elements("word")
            select wordElement.Value
        ).ToList()
    }
 ).ToList();




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签