简体   繁体   English

C#中与XML的进程间通信

[英]Inter-process communication with XML in C#

I have two applications: X and Y. X has a set of variables (stored in an object of a class), which have to be transferred to Y. I am planning to use an XML-file as a record stored on disk that can be accessed by name by both applications. 我有两个应用程序:X和Y。X有一组变量(存储在类的对象中),必须将这些变量传递给Y。我计划将XML文件用作存储在磁盘上的记录,这两个应用程序都可以通过名称访问。 X writes the data to that XML-file and Y reads it. X将数据写入该XML文件,Y读取它。

I thought I can use XmlSerializer (System.Xml.Serialization) to accomplish this. 我以为可以使用XmlSerializer(System.Xml.Serialization)来完成此任务。 With XmlSerializer I can create an XML file that looks like this: 使用XmlSerializer,我可以创建一个如下所示的XML文件:

<MonsterCollection>
  <Monsters>
    <Monster name="a">
     <Health>5</Health>
    </Monster>
    <Monster name="b">
     <Health>3</Health>
    </Monster>
  </Monsters>
</MonsterCollection>

When Y reads this XML file, it does not know the actual data type of variables Health . 当Y读取此XML文件时,它不知道变量Health的实际数据类型。 Therefore, the original class has to be defined in both X and Y. Is there a way to store the datatypes in the XML-file, as well? 因此,必须同时在X和Y中定义原始类。是否有办法将数据类型存储在XML文件中? In the end, I would like to accomplish something like this: 最后,我想完成以下工作:

<Monster name="a" type="" help="This is a monster">
  <var name="Health" type="uint16" val="5" help="Healthiness of this monster" /> 
</Monster>
<Monster name="b" type="" help="This is a monster">
  <var name="Health" type="uint16" val="3" help="Healthiness of this monster" /> 
</Monster>

you can use the below mentioned code 您可以使用下面提到的代码

 public class Monster 
{
    [XmlAttribute("name")]
    public string name {get;set;}
    [XmlAttribute("type")]
    public string type {get;set;}
    [XmlAttribute("val")]
    public int val { get; set; }
}

for ref serialize-object-to-element 用于ref 序列化对象到元素

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

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