如何遍历xml子元素 [英] how to iterate through xml child elements

查看:111
本文介绍了如何遍历xml子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have this xml tag code which looks as follows:

<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页面中通过以下方式在序列化的帮助下使用它:

I use this in .Cs page with help of serialization in the following way:

 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'呈现两次.如何通过遍历子元素使第二个填充符号显示

For the second child element SimpleFillSymbol of Fvalue 'V' I unable to get it show during the rendering. The SimpleFillSymbol value 'A' renders twice as shown in the table of contents..How can i make the second fill symbol to show by iterating through the child elements

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

Code for the serilaization of the xml to object:

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

}

推荐答案

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

You need to change the classes for XML deserialization as follows:

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对象的列表

Use the following code to fetch the list of SimpleFillSymbol objects

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

    }

}

这篇关于如何遍历xml子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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