读取XML并在C#中动态创建对象 [英] Read XML and create objects dynamically in C#

查看:684
本文介绍了读取XML并在C#中动态创建对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想从XML填充对象(我不知道对象名称,但是它可以是collection/Dataset/Datatable或任何对象).
例如我下面有XML

Hello All,

I want to populate object (I don''t know the object name but it could be collection/Dataset / Datatable or any) from XML .
For example I have below XML

<Employees Count="2">
 <Employee Code="A" Name="XYZ">
   <Address City="A" Pin="0000"></Address>
   <Contact Mob="123456" Phone="02145235" Email="XYZ@domain.com"></Contact>
 </Employee>
 <Employee Code="B" Name="ABC">
   <Address City="C" Pin="111"></Address>
   <Contact Mob="78794656" Phone="+912546" Email="ABC@domain.com"></Contact>
 </Employee>
</Employees>



结果是我想在任何具有这样的C#对象中输出



in the result of that I want output in any C# object which have like this

Employees 
   [Count=2]
  Employee    
     [Code=A] [Name=XYZ]
     Address
     [City=A] [Pin=0000]
     Contact 
     [Mob=123456] [Phone=02145235] [Email=XYZ@domain.com]
  Employee
   [Code=B] [Name=ABC]
   Address
   [City=C] [Pin=111]
   Contact
   [Mob=78794656] [Phone=+912546] [Email=ABC@domain.com]



这样我就可以读为
MyObject.Employees.Count
MyObject.Employee [0] .Code [将给我A]
MyObject.Employee [1] .Code [将给我B]

...等等

阿米尔·马富兹(Amir Mahfoozi)您有什么主意吗



So that I can read through as
MyObject.Employees.Count
MyObject.Employee[0].Code [which will give me A]
MyObject.Employee[1].Code [which will give me B]

...And so on

Amir Mahfoozi do you have any idea

推荐答案

我希望这会解决您的问题:

I hope this will solve your problem:

[XmlRoot("Datatable"), Serializable]
public class Datatable
{
    [XmlElement("Employees")]
    public Employees Employees { get; set; }
}

public class Employees
{
    [XmlAttribute("Count")]
    public int Count { get; set; }
    [XmlElement("Employee")]
    public List<employee> Employee { get; set; }
}

public class Employee
{
    [XmlAttribute("Code")]
    public string Code { get; set; }
    [XmlAttribute("Name")]
    public string Name { get; set; }
    [XmlElement("Address")]
    public Address Address { get; set; }
    [XmlElement("Contact")]
    public Contact Contact { get; set; }
}

public class Address
{
    [XmlAttribute("City")]
    public string City { get; set; }
    [XmlAttribute("Pin")]
    public string Pin { get; set; }
    [XmlText]
    public string AddressValue { get; set; }
}

public class Contact
{
    [XmlAttribute("Mob")]
    public string Mob { get; set; }
    [XmlAttribute("Phone")]
    public string Phone { get; set; }
    [XmlAttribute("Email")]
    public string Email { get; set; }
    [XmlText]
    public string ContactValue { get; set; }
}




要从文件中加载对象,请执行以下操作:




To load the object from the file:

XmlSerializer serializer = new XmlSerializer(typeof(Datatable));
FileStream loadStream = new FileStream("sample.xml", FileMode.Open, FileAccess.Read);
Datatable loadedObject = (Datatable)serializer.Deserialize(loadStream);
loadStream.Close();



和sample.xml需要看起来像这样:



and sample.xml need to look like this:

<?xml version="1.0"?>
<datatable xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <employees count="2">
    <employee code="A" name="XYZ">
      <address city="A" pin="0000" />
      <contact mob="123456" phone="02145235" email="XYZ@domain.com" />
    </employee>
    <employee code="B" name="ABC">
      <address city="C" pin="111" />
      <contact mob="78794656" phone="+912546" email="ABC@domain.com" />
    </employee>
  </employees>
</datatable>


您的问题是众所周知的,通常可以通过对象序列化解决.例如,请参见:序列化(C#和Visual Basic)" [如何:从XML文件(C#和Visual Basic)读取对象数据" [
Your problem is a well known one, and it is usually addressed via object serialization. See, for instance: "Serialization (C# and Visual Basic)"[^], "How to: Read Object Data from an XML File (C# and Visual Basic)"[^] at MSDN.


我认为你可以借助system.dynamic命名空间以及dynamic和ExpendoObject类来加载xml文件以创建运行时对象.


i think you can take the help of system.dynamic namespace and dynamic and ExpendoObject classes to load the xml file to create your runtime objects.


http://blogs.captechconsulting.com/blog/kevin-hazzard/fluent-xml-parsing-using-cs-dynamic-type-part-1?cf03388EF1=29488A758!MTA1MDM0ODc3OmNvcnByYWRpdXNzc286EVZo9M86sPZ+IK01HmjHZQ==[^]


link can help you if i understand your requirement correctly.


这篇关于读取XML并在C#中动态创建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆