如何 XML 序列化由固定文本和属性值组成的字符串? [英] How to XML serialise a string, composed of fixed text and attribute values?

查看:18
本文介绍了如何 XML 序列化由固定文本和属性值组成的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前的问题中,我询问了如何通过 XML 序列化对象变成类似的东西:

In my previous question, I asked how to XML serialise an object into something like:

<PO X=0 Y=0 ... />

我的对象如下所示:

public class Offset
{
    public int X { get; set; }
    public int Y { get; set; }

正如评论和答案中所述,这是无效的 XML,因此尝试将对象序列化为 XML 是没有意义的.

As stated in the comments and in the answer, this is invalid XML, so it makes no sense trying to XML serialise an object into that.

客户同意另一种格式(有效的 XML):

The customer agreed to another format (which is valid XML):

<PO>X=0 Y=0 ...</PO>

但是如何做到这一点?

我看到了三种方法来完成这项工作:

I see three ways to get this done:

  • 使用 XML 序列化器指令,如 [XmlRoot][XmlElement][XmlAttribute] 和其他指令来引导 XML 序列化器.
  • 创建一个新属性并将其用于 XML 序列化.
  • 两者的结合.
  • Use XML serialiser instructions, like [XmlRoot], [XmlElement], [XmlAttribute] and others to steer the XML serialiser.
  • Create a new attribute and use that one for the XML serialisation.
  • A combination of both.

我尝试了第一种方法但失败了,所以我决定创建一个新属性,这就是我目前拥有的:

I tried the first approach but I failed, so I decided to create a new attribute, this is what I currently have:

[XmlRoot(ElementName = "PO")]
public class Offset
{
    [XmlIgnore]
    public double X { get; set; }
    [XmlIgnore]
    public double Y { get; set; }
    [XmlIgnore]
    public double Z { get; set; }
    [XmlIgnore]
    public double Phi { get; set; }
    [XmlElement(ElementName = "output")]
    public string xml_output;

    public string generate_xml_output()
    { return $"X={X} Y={Y} Z={Z} PHI={Phi}"; }

    public Offset()
    { X = 0; Y = 0; Z = 0; Phi = 0;
      xml_output = generate_xml_output(); }

这是我当前的 XML:

This is my current XML:

<PO>
  <output>X=0 Y=0 Z=0 PHI=0</output>
</PO>

这是接近但没有雪茄:-)

This is close but no cigares :-)

有谁知道我如何告诉 XML 序列化程序使用 output 属性作为默认属性"?(为了省略 标签)?

Does anybody know how I can tell the XML serialiser to use the output attribute as "default attribute" (in order to omit the <output> tags)?

提前致谢

事后编辑

乍一看,一切似乎都很好,但是 setter 现在出现了问题.我自己写了一个解析器,但我的印象是,虽然它看起来不错,但缺少一些东西,并且弄乱了我的 XML 序列化.

At first sight, everything seems to be fine, but the setter is giving problems now. I've written a parser myself, but I have the impression that, although it looks good, something is missing, and messing up my XML serialisation.

这是我的类(使用我的 setter 实现)的样子:

This is what my class (with my implementation of the setter) looks like:

        [XmlText]
        public string xml_output
        {
            get { return $"X={X} Y={Y} Z={Z} PHI={Phi}"; }
            set { 
                  string[] temp = xml_output.Split(' ');
                  try 
                  { X = Convert.ToDouble(temp[0].Replace("X=","")); } 
                  catch (Exception) 
                  { X = 0; }
                  try
                  { Y = Convert.ToDouble(temp[1].Replace("Y=", "")); }
                  catch (Exception)
                  { Y = 0; }
                  try
                  { Z = Convert.ToDouble(temp[2].Replace("Z=", "")); }
                  catch (Exception)
                  { Z = 0; }
                  try
                  { Phi = Convert.ToDouble(temp[3].Replace("PHI=", "")); }
                  catch (Exception)
                  { Phi = 0; }
                }
        }

你知道我的 setter 缺少什么吗?

Do you know what my setter is missing?

推荐答案

[XmlText]
public string CompositeOutput {
    get { return $"X={X} Y={Y} Z={Z} PHI={Phi}"; }
    set { /* your parse logic here *}
}

这篇关于如何 XML 序列化由固定文本和属性值组成的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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