简体   繁体   English

将XML文档反序列化为通讯录

[英]Deserializing an XML document into an Addressbook

I have searched through multiple examples at this point all detailing different methods of Deserializing, Serializing or even other random unrelated processes which I probably should have ignored but kept reading anyway out of desperation. 在这一点上,我已经搜索了多个示例,所有这些示例都详细说明了反序列化,序列化甚至其他随机无关过程的不同方法,我可能应该忽略这些方法,但是无论如何他们都会出于绝望而继续阅读。

I want to Deserialize this XML document: 我想反序列化此XML文档:

<?xml version="1.0" encoding="UTF-8"?>
<AddressBook>
<Contact>
    <Name>Steve Man</Name>
        <ContactType>Family</ContactType>
        <DateofBirth>1992-07-10T00:00:00</DateofBirth>
        <AddressLine1>123 Fake Street</AddressLine1>
        <AddressLine2>New Worthington</AddressLine2>
        <AddressLine3>Regalpark</AddressLine3>
        <AddressLine4 />
        <Postcode>RP3 6BG</Postcode>
        <Email1>Steve@gmail.com</Email1>
        <Email2>Steve@tiscali.co.uk</Email2>
        <Phone>01422951283</Phone>
        <Mobile>07559213822/Mobile>
        <AdditionalInfo>This is that weird guy.</AdditionalInfo>
</Contact>

Into the above mentioned AddressBook object in a C# windows form application, the contents of which will come from the data in the XML document. 进入上述C#Windows窗体应用程序中的AddressBook对象,其内容将来自XML文档中的数据。

After being Deserialized (or so I'm told) I can then use the objects it makes from this process directly with the Application itself, meaning I can Display/Edit/Delete them as need be. 在反序列化(或告诉我)之后,我可以直接将其在此过程中创建的对象与Application本身一起使用,这意味着我可以根据需要显示/编辑/删除它们。

Then after edits have been made the new data would need to be Serialized back into the original XML format for storage. 然后,在进行编辑后,将需要将新数据序列化回原始XML格式以进行存储。

I apologize if this comes across as a stupid/duplicate question but I'm pretty new to C# and have had a long time of reading and struggling and would greatly appreciate any input you can provide, if the question needs any adjusting or you need more info then I will be happy to provide it. 如果这是一个愚蠢/重复的问题,我深表歉意,但是我对C#还是很陌生,并且阅读和挣扎的时间很长,如果问题需要任何调整或需要更多帮助,我们将不胜感激。信息,那么我很乐意提供。

First of all I would like to point you to this page. 首先,我想指出您这一页。 http://azuliadesigns.com/xml-serialization-deserialization/ http://azuliadesigns.com/xml-serialization-deserialization/

You should use this code to serialize a list of Contact objects. 您应该使用此代码序列化Contact对象的列表。 I believe below code should do the trick for you 我相信下面的代码可以为您解决问题

XmlSerializer mySerializer = new XmlSerializer(typeof(List<Contact>));
    StreamWriter myWriter = new StreamWriter("c:/addressbook.xml");
    mySerializer.Serialize(myWriter, test);
    myWriter.Close();

And to deserialize 并反序列化

  List<Contact> test;

  XmlSerializer mySerializer = new XmlSerializer(typeof(List<Contact>));
  FileStream myFileStream = new FileStream("c:/addressbook.xml",FileMode.Open);

  test = (List<Contact>)mySerializer.Deserialize(myFileStream);

I didn't change the variable names to make it as clear as possible. 我没有更改变量名称以使其尽可能清晰。 I use this method to serialize and deserialize my settings object. 我使用此方法来序列化和反序列化我的设置对象。

And your Contact class should look like 您的Contact类应该看起来像

[Serializable]
public class Contact
{
    public string Name "";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM