How to: Read Object Data from an XML File

时间:2022-09-23 18:05:34

This example reads object data that was previously written to an XML file using the XmlSerializer class.

Example

This code example is also available as an IntelliSense code snippet. In the code snippet picker, it is located in XML. For more information, see Code Snippets.

public class Book
{
public String title;
} public void ReadXML()
{
// First write something so that there is something to read ...
var b = new Book { title = "Serialization Overview" };
var writer = new System.Xml.Serialization.XmlSerializer(typeof(Book));
var wfile = new System.IO.StreamWriter(@"c:\temp\SerializationOverview.xml");
writer.Serialize(wfile, b);
wfile.Close(); // Now we can read the serialized book ...
System.Xml.Serialization.XmlSerializer reader =
new System.Xml.Serialization.XmlSerializer(typeof(Book));
System.IO.StreamReader file = new System.IO.StreamReader(
@"c:\temp\SerializationOverview.xml");
Book overview = (Book)reader.Deserialize(file);
file.Close(); Console.WriteLine(overview.title); }

Compiling the Code

Replace the file name "c:\IntroToVB.xml" with the name of the file containing the serialized data. For more information about serializing data, see How to: Write Object Data to an XML File (C# and Visual Basic).

The class must have a public constructor without parameters.

Only public properties and fields are deserialized.

Robust Programming

The following conditions may cause an exception:

  • The class being serialized does not have a public, parameterless constructor.

  • The data in the file does not represent data from the class to be deserialized.

  • The file does not exist (IOException).

.NET Framework Security

Always verify inputs, and never deserialize data from an untrusted source.

The re-created object runs on a local computer with the permissions of the code that deserialized it.

Verify all inputs before using the data in your application.

How to: Read Object Data from an XML File的更多相关文章

  1. How to: Write Object Data to an XML File

    This example writes the object from a class to an XML file using the XmlSerializer class. Namespace: ...

  2. RCE via XStream object deserialization && SECURITY-247 / CVE-2016-0792 XML reconstruction Object Code Inject

    catalogue . Java xStream . DynamicProxyConverter . java.beans.EventHandler . RCE via XStream object ...

  3. uiautomator:Error while obtaining UI hierarchy XML file: com.android.ddmlib.SyncException: Remote object doesn't exist!

    尝试用android sdk的uiautomatorviewer抓元素的时候报错:Error while obtaining UI hierarchy XML file: com.android.dd ...

  4. uiautomatorviewer 报错 Error while obtaining UI hierarchy XML file: com.android.ddmlib.SyncException: Remote object doesn't exist!

    在进行自动化时经常需要使用到 uiautomatorviewer获取控件的各个属性,然后在脚本中通过各个控件的属性来操作. 如果使用的是uiautomator2的话,一般都是使用weditor这个来查 ...

  5. 【Android】【问题解决记录】Error obtaining UI hierarchy :Error while obtaining UI hierarchy XML file: com.android.ddmlib.SyncException: Remote object doesn't exist!

    在使用uiautomatorviewer时遇到两类Error obtaining UI hierarchy报错,分别是: Error while obtaining UI hierarchy XML ...

  6. Error while obtaining UI hierarchy XML file: com.android.ddmlib.SyncException: Remote object doesn't

    在使用真机定位页面元素时启动uiautomatorviewer.bat ,报错Error while obtaining UI hierarchy XML file: com.android.ddml ...

  7. [vb.net]XML File Parsing in VB.NET

    Introduction Parsing XML files has always been time consuming and sometimes tricky. .NET framework p ...

  8. error while obtaining ui hierarchy xml file...用 uiautomatorviewer 获取安卓手机软件页面时报错

    Error while obtaining UI hierarchy XML file: com.android.ddmlib.SyncException: Remote object doesn't ...

  9. 报错:Binary XML file line #7: Error inflating class android.support.v7.widget.RecyclerView

    近期学习RecyclerView,使用eclipse引用RecyclerView.编写完demo后编译没有问题,一执行就挂掉,错误例如以下: 07-22 23:05:34.553: D/Android ...

随机推荐

  1. 深入理解Objective-C Runtime

    一.简介 主要特点: 在OC语言中,函数的调用是属于动态调用的,编译阶段并不确定要调用的函数,在真正的运行时才会根据函数名查找要调用哪个函数. 而在C语言中,函数的调用是在编译阶段就已经确定要调用哪个 ...

  2. 在windows7下安装CentOS

    需要用到的软件 EasyBCD 设置索引菜单 PA5.2_Portable 分区助手 WinGrub 查看硬盘代号 1.使用分区助手,腾出至少4GB的空间,并格式化为fat32格式,将CentOS的I ...

  3. SGU 113

    113. Nearly prime numbers time limit per test: 0.25 sec. memory limit per test: 4096 KB Nearly prime ...

  4. eclipse/MyEclipse 日期格式、注释日期格式、时区问题[转]

    http://www.cnblogs.com/hoojo/archive/2011/03/21/1990070.html 在eclipse/MyEclipse中,如果你的注释或是运行System.ou ...

  5. 用三或四个个div标签实现工字效果

    使用重构的方式制作出一个如下图的水平.垂直都居中,短边为50px,长边为150px的红色“工”字. a) 使用3个div完成 <!DOCTYPE html><html lang=&q ...

  6. linux删除、移动、拷贝时,加-f仍然会提示的解决办法

    cp -f 还是提示 root# alias 可以看到,执行cp就等于执行了cp -i,-i是确认提示 alias cp='cp -i' root# vi ~/.bashrc 修改完毕Esc, :wq ...

  7. Linux CentOS7 开启80,443端口外网访问权限

    一.查看系统防火墙状态(如果返回 running 代表防火墙启动正常) firewall-cmd --state 二.开启端口外网访问 1.添加端口 返回 success 代表成功(--permane ...

  8. sql server和oracle数据库

    sql server和oracle数据库安装按照官方教程即可:以及他们相应的管理工具,sql server management studio自带的,oracle的管理工具PLSQL需要单独下载安装, ...

  9. docker toolbox在win7下的安装

    1.下载安装docker toolbox docker toolbox的下载地址: http://mirrors.aliyun.com/docker-toolbox/windows/docker-to ...

  10. phpsession配置

    1. 介绍 1.1 作用: 主要用于服务器端的会话保持. 1.2 结构: Session分以下几部分: 1)Session id 用户Session的唯一标识(随机生成,具有唯一性,随机性) 2)Se ...