簡體   English   中英

如何遍歷xml子元素

[英]how to iterate through xml child elements

我有這個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; }       
}

}

嘗試使用linq:

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

將返回元素SimpleFillSymbol的兩個Fvalue屬性

您需要更改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);

    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM