Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将XmlAttribute解析为复杂类型_C#_Xml_Svg - Fatal编程技术网

C# 将XmlAttribute解析为复杂类型

C# 将XmlAttribute解析为复杂类型,c#,xml,svg,C#,Xml,Svg,我正在尝试创建SVG格式的对象模型。因此,由于它是XML,所以我使用XmlSerializer。但我有个问题。有一些xml属性名为“style”。它看起来像复杂类型,但表示为字符串。以下是此属性的示例: style=“填充:无;笔划:00ff00;笔划宽度:8.7489996;笔划斜接限制:4;笔划不透明度:1;笔划虚线阵列:无;笔划虚线偏移:8.74900006” 如您所见,有“填充”、“笔划”、“笔划宽度”等属性。 我写了这堂课 public class SvgStyle { pub

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

style=“填充:无;笔划:00ff00;笔划宽度:8.7489996;笔划斜接限制:4;笔划不透明度:1;笔划虚线阵列:无;笔划虚线偏移: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接口实现和OnSerialization/OnDeserialization方法,但我得到的只是另一个异常。 有没有办法将这个字符串反序列化到我的类中?
谢谢。

我相信您需要在SVGStyle类上实现IXmlSerializable接口

读到你已经试过了;对不起

    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));

    }
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Xml.Serialization;
命名空间XML类型
{
公共类SvgStyle:IXmlSerializable
{
私有常量字符串fillColorKey=“fill”;
私有常量字符串strokeColorKey=“stroke”;
私有常量字符串strokeWidthKey=“笔划宽度”;
私有常量字符串strokeMiterLimitKey=“strokeMiterLimit”;
私有常量字符串strokeopatitykey=“笔划不透明度”;
私有常量字符串strokeDashArrayKey=“strokedasharray”;
private const string strokeDashOffsetKey=“stroke dashoffset”;
[XmlAttribute]
公共字符串样式{get;set;}
公共字符串FillColor{get;set;}
公共字符串StrokeColor{get;set;}
公共浮点StrokeWidth{get;set;}
public int StrokeMiterlimit{get;set;}
公共浮点StrokeOpacity{get;set;}
公共浮点StrokeDashoffset{get;set;}
公共字符串StrokeDasharray{get;set;}
public System.Xml.Schema.XmlSchema GetSchema()
{
返回null;
}
public void ReadXml(System.Xml.XmlReader)
{
reader.MoveToContent();
Style=reader.GetAttribute(“Style”);
Dictionary lookupTable=ParseAttribute(样式);
FillColor=lookupTable[fillColorKey];
StrokeColor=lookupTable[strokeColorKey];
float strokeWidthFloatValue=0;
TryParse(lookupTable[strokeWidthKey],out strokeWidthFloatValue);
StrokeWidth=strokeWidthFloatValue;
int-strokeMiterLimitInt=0;
Int32.TryParse(可查找[strokeMiterLimitKey],out strokeMiterLimitInt);
StrokeMiterlimit=StrokeMiterlimit;
int strokeOpacityInt=0;
Int32.TryParse(可查找[strokeMiterLimitKey],外strokeOpacityInt);
StrokeOpacity=strokeOpacityInt;
int strokeDashOffsetInt=0;
Int32.TryParse(可查找[strokeMiterLimitKey],out strokedashofsett);
StrokeDashoffset=StrokeDashoffset;
StrokeDasharray=lookupTable[strokeDashArrayKey];
布尔值isEmptyElement=reader.isEmptyElement;//(1)
reader.ReadStartElement();
}
public void WriteXml(System.Xml.XmlWriter)
{
WriteAttributeString(“风格”,风格);
}
私有字典属性(字符串属性)
{
字符串[]arr=attribute.Split(“;”);
Dictionary dic=新字典();
对于(int i=0;i
实现IXmlSerializable接口给了我另一个异常“无法序列化Svg.SvgStyle.XmlAttribute/XmlText类型的成员'Style',无法用于编码实现IXmlSerializable的类型”。那么,你能详细说明我应该如何实施它吗?我设置了类的所有属性和它的属性,如本例所示。在您的示例中,合并了两个不同的实体,在我的示例中是SvgGraphicElement和SvgStyle。也许这是我的错误,但“style”是svggraphic元素的属性,并没有SvgStyle元素。目前看来我需要为SvgGraphicElement类实现IXmlSerializable Interbase,但不是SvgStyle。您能提供xml吗?这是示例xml:
image/svg+xml