如何遍历xml子元素

时间:2014-08-17 16:47:32

标签: c# xml-serialization gis

我有这个xml标记代码,如下所示:

<FeatureLayerExtension>
    <WhereString>(ZONE ='A') or (ZONE ='V')</WhereString>
    <OutFields>ZONE</OutFields>
    <UniqueDataCount>2</UniqueDataCount>
    <UniqueValueRenderer>
        <SimpleFillSymbol  Color="White" Fill="Yellow" Width="1.5" Fvalue ='A'  />
        <SimpleFillSymbol  Color="White" Fill="Green" Width="1.5" Fvalue ='V' />
    </UniqueValueRenderer>
</FeatureLayerExtension>

我在.Cs页面中使用以下方式帮助序列化:

 if (projectMap.FeatureLayerConfig != null && projectMap.FeatureLayerConfig.UniqueValueRenderer != null)
        {
           agisFeatureLayer.RendererTakesPrecedence = true;                
            var renderer = new UniqueValueRenderer();
            renderer.Field = projectMap.FeatureLayerConfig.OutFields;


            for (int i = 0; i <= projectMap.FeatureLayerConfig.UniqueDataCount - 1; i++)
            {
                UniqueValueInfo info = new UniqueValueInfo();

                if (projectMap.FeatureLayerConfig.UniqueValueRenderer.SimpleFillSymbol != null)
                {
                    var fill = GlobalConfigs.ColorToStringDic[projectMap.FeatureLayerConfig.UniqueValueRenderer.SimpleFillSymbol.Fill];
                    var borderBrush = GlobalConfigs.ColorToStringDic[projectMap.FeatureLayerConfig.UniqueValueRenderer.SimpleFillSymbol.Color];
                    var borderThickness = projectMap.FeatureLayerConfig.UniqueValueRenderer.SimpleFillSymbol.Width;
                    var fillSymbol = new SimpleFillSymbol() { Fill = new SolidColorBrush(fill), BorderBrush = new SolidColorBrush(borderBrush), BorderThickness = borderThickness };


                    info.Value = PojectMap.FeatureLayerConfig.UniqueValueRenderer.UniqueValueInfo.SimpleFillSymbol.Fvalue;
                    info.Symbol = fillSymbol;
                    renderer.Infos.Add(info);

                }

            }

            agisFeatureLayer.Renderer = renderer;
        }
    } 

对于Fvalue'V'的第二个子元素SimpleFillSymbol,我无法在渲染过程中显示它。 SimpleFillSymbol值'A'呈现两次,如内容列表所示。如何通过迭代子元素来显示第二个填充符号

将xml序列化为对象的代码:

 using System.Xml.Serialization;
 using System.Windows.Media;

 namespace My.GIS.Viewer.Configuration.Map
  {
public partial class PortalFeatureLayer
{

    [XmlElement(typeof(uvRendererConfig), ElementName = "UniqueValueRenderer")]
    public uvRendererConfig UniqueValueRenderer { get; set; }

    [XmlElement(typeof(int), ElementName = "UniqueDataCount")]
    public int UniqueDataCount { get; set; }

}


public class uvRendererConfig
{

    [XmlElement(typeof(mySimpleFillSymbol), ElementName = "SimpleFillSymbol")]
    public mySimpleFillSymbol SimpleFillSymbol { get; set; }

    [XmlElement(typeof(mySimpleMarkerSymbol), ElementName = "SimpleMarkerSymbol")]
    public mySimpleMarkerSymbol SimpleMarkerSymbol { get; set; }

    [XmlElement(typeof(mySimpleLineSymbol), ElementName = "SimpleLineSymbol")]
    public mySimpleLineSymbol SimpleLineSymbol { get; set; }

}





public class mySimpleFillSymbol : SymbolBase {
    [XmlAttribute(AttributeName = "Fill")]
    public string Fill { get; set; }

    [XmlAttribute(AttributeName = "FieldValue")]
    public string FieldValue { get; set; }


}


public class SymbolBase
{
    [XmlAttribute(AttributeName = "Color")]
    public string Color { get; set; }

    [XmlAttribute(AttributeName = "Width")]
    public double Width { get; set; }       
}

}

2 个答案:

答案 0 :(得分:1)

尝试使用linq:

XElement xelement = XElement.Load(document path);
var ell = from e in xelement.Descendants("SimpleFillSymbol")
          select e.Attribute("Fvalue");

将返回元素SimpleFillSymbol

的两个Fvalue属性

答案 1 :(得分:1)

您需要更改XML反序列化的类,如下所示:

namespace My.GIS.Viewer.Configuration.Map
{
    [Serializable()]
    public class FeatureLayerExtension
    {

        public string WhereString{ get; set; }

        public string OutFields { get; set; }

        public string UniqueDataCount { get; set; }

        //SimpleFillSymbol should be a list   
        [XmlElement("SimpleFillSymbol")]
        public List<SimpleFillSymbol> SimpleFillSymbolList = new List<SimpleFillSymbol>();

    }

    [Serializable()]
    public class SimpleFillSymbol
    {
        [XmlAttribute("Color")]
        public string Color { get; set; }

        [XmlAttribute("Fill")]
        public string Fill { get; set; }

        [XmlAttribute("Width")]
        public string Width { get; set; }

        [XmlAttribute("Fvalue")]
        public string Fvalue { get; set; }

    }

}

使用以下代码获取SimpleFillSymbol对象列表

public class DeserializeFeatureLayerExtension
{

    public void desiralizeXML()
    {
        XmlSerializer fledeserializer = new XmlSerializer(typeof(FeatureLayerExtension));
        TextReader reader = new StreamReader(file path);
        FeatureLayerExtension fleData = (FeatureLayerExtension)deserializer.Deserialize(reader);
        reader.Close();
    //Fetch the list of SimpleFillSymbol objects
    List<SimpleFillSymbol> listSimpleFillSymbol = (from e in fleData
                                                     select e.SimpleFillSymbolList);

    }

}