将XmlAttribute解析为复杂类型

时间:2014-08-01 16:34:58

标签: c# xml svg

我正在尝试创建SVG格式的对象模型。因此,就像XML一样,我正在使用XmlSerializer。但我有一个问题。有一些名为“style”的xml属性。它看起来像复杂类型,但表示为字符串。有这个属性的例子:

  

风格= “填充:无;行程:#00FF00;笔划宽度:8.7489996;卒中miterLimit分别:4;中风不透明度:1;卒中dasharray:无;卒中dashoffset:8.74900006”

正如你所看到的,有“填充”,“笔画”,“笔画宽度”等属性。 我写了班级

public class SvgStyle
{
    public string FillColor { get; set; }
    public string StrokeColor { get; set; }
    public float StrokeWidth { get; set; }
    public int StrokeMiterlimit { get; set; }
    public float StrokeOpacity { get; set; }
    public float StrokeDashoffset { get; set; }
    public string StrokeDasharray { get; set; }
}

和另一个班级

public abstract class SvgGraphicElement
{
    [XmlAttribute("style")]
    public SvgStyle Style { get; set; }
}

我得到的只是例外

  

无法序列化Svg.SvgStyle类型的成员'Style'。 XmlAttribute / XmlText不能用于编码复杂类型。

我尝试使用IXmlSerializable接口实现和OnSerializing / OnDeserializing方法,但我得到的是另一个例外。 有没有办法将这个字符串反序列化到我的班级? 感谢。

1 个答案:

答案 0 :(得分:0)

我相信您需要在SVGStyle类

上实现IXmlSerializable接口

http://msdn.microsoft.com/en-us/library/system.xml.serialization.ixmlserializable.aspx

刚看到你已经尝试过了;抱歉。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml.Serialization;

namespace XmlTypes
{
  public class SvgStyle : IXmlSerializable
  {

    private const string fillColorKey = "fill";
    private const string strokeColorKey ="stroke";
    private const string strokeWidthKey = "stroke-width";
    private const string strokeMiterLimitKey = "stroke-miterlimit";
    private const string strokeOpacityKey ="stroke-opacity";
    private const string strokeDashArrayKey="stroke-dasharray";
    private const string strokeDashOffsetKey = "stroke-dashoffset";

    [XmlAttribute]
    public String Style { get; set; }
    public string FillColor { get; set; }
    public string StrokeColor { get; set; }
    public float StrokeWidth { get; set; }
    public int StrokeMiterlimit { get; set; }
    public float StrokeOpacity { get; set; }
    public float StrokeDashoffset { get; set; }
    public string StrokeDasharray { get; set; }


    public System.Xml.Schema.XmlSchema GetSchema()
    {
      return null;
    }

    public void ReadXml(System.Xml.XmlReader reader)
    {
      reader.MoveToContent();
      Style = reader.GetAttribute("style");
      Dictionary<string, string> lookupTable = ParseAttribute(Style);

      FillColor = lookupTable[fillColorKey];
      StrokeColor = lookupTable[strokeColorKey];

      float strokeWidthFloatValue=0; 
      float.TryParse(lookupTable[strokeWidthKey] , out strokeWidthFloatValue) ;
      StrokeWidth = strokeWidthFloatValue;

      int strokeMiterLimitInt = 0;
      Int32.TryParse(lookupTable[strokeMiterLimitKey] , out strokeMiterLimitInt);
      StrokeMiterlimit = strokeMiterLimitInt;

      int strokeOpacityInt = 0;
      Int32.TryParse(lookupTable[strokeMiterLimitKey], out strokeOpacityInt);
      StrokeOpacity = strokeOpacityInt;

      int strokeDashOffsetInt = 0;
      Int32.TryParse(lookupTable[strokeMiterLimitKey], out strokeDashOffsetInt);
      StrokeDashoffset = strokeDashOffsetInt;

      StrokeDasharray = lookupTable[strokeDashArrayKey];


      Boolean isEmptyElement = reader.IsEmptyElement; // (1)
      reader.ReadStartElement();

    }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
      writer.WriteAttributeString("style", Style);

    }
    private Dictionary<string, string> ParseAttribute(string attribute)
    {
      string[] arr = attribute.Split(';');
      Dictionary<string, string> dic = new Dictionary<string, string>();
      for(int i = 0; i < arr.Length; i++)
      {
        string[] arrItem = arr[i].Split(':');
        dic.Add(arrItem[0], arrItem[1]);
      }
      return dic;
    }    
  }
}





 [TestMethod]
    public void TestMethod1()
    {
      string xml = @"<SvgStyle style='fill:none;stroke:#00ff00;stroke-width:8.7489996;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;stroke-dashoffset:8.74900006'></SvgStyle>";

      XmlSerializer x = new XmlSerializer(typeof(SvgStyle));
      SvgStyle myTest = (SvgStyle)x.Deserialize(new StringReader(xml));

    }