【问题标题】:how to iterate through xml child elements如何遍历xml子元素
【发布时间】:2014-08-17 16:47:31
【问题描述】:

我有这个 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; }       
}

}

【问题讨论】:

    标签: c# xml-serialization gis


    【解决方案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);
    
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      尝试使用 linq:

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

      将返回元素 SimpleFillSymbol 的两个 Fvalue 属性

      【讨论】:

      • 谢谢Nipun的回复,但我已经有一个单独的序列化文件..如果你看到发布的.cs文件的代码你就会知道。所以我想这里不能使用 XML 来 linq 代码。所以基本上我需要这样的 Foreach(var child in projectMap.FeatureLayerConfig.UniqueValueRenderer) {}..someting like this
      • 所以你的意思是说你已经将 XML 文件序列化为一个对象,在这种情况下你也可以使用 LINQ to Objects
      • HI Nipun..感谢您跟上我..我已经编辑了我的问题,我已经发布了我的序列化代码..希望这能让您更好地理解我的问题..我还是新手到所有 LINQ to objects 概念..
      • 学习 LINQ 的基础知识非常容易。使用 linq,您可以查询任何类型的数据存储。如果您可以在这里学习 LINQ,那就太好了:msdn.microsoft.com/en-us/library/bb397926.aspx 和 Linq To Objects 在这里:codeproject.com/Articles/26743/Using-LINQ-to-Objects-in-C。我将为您的问题添加另一个答案。
      • 非常感谢Nipun。对我帮助很大。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-23
      • 1970-01-01
      • 1970-01-01
      • 2012-07-26
      • 1970-01-01
      • 2013-07-20
      相关资源
      最近更新 更多