繁体   English   中英

Silverlight 4.0 读取复合体 xml 与 linq

[英]Silverlight 4.0 reading complex xml with linq

我坚持以下 XML 问题。 这是我的 XML 文件:

<POIs lastUsedId="9000010">
<POI id="9000010" name="München"><Latitude>48.139126</Latitude><Longitude>11.5801863</Longitude>
<Address>muenchen</Address><PhotoDescription>Hofbräuhaus</PhotoDescription>
<Photos directory="_x002F_pics"><PhotoFile>pic4poi_9000010-01.jpg</PhotoFile> 
<PhotoFile>pic4poi_9000010-02.jpg</PhotoFile><PhotoFile>pic4poi_9000010-03.jpg</PhotoFile>
<PhotoFile>pic4poi_9000010-04.jpg</PhotoFile></Photos>
<InformationFile>infos\info4poi_9000010.txt</InformationFile></POI>
</POIs>

这是我读取文件的代码:

XDocument doc = XDocument.Load(s);
                lastID = Int32.Parse(doc.Root.Attribute("lastUsedId").Value.ToString());
                CultureInfo cultureInfo = new CultureInfo("en-GB");
                var pois = from res in doc.Descendants("POI")
                           select new
                           {
                               id = Int32.Parse(res.Attribute("id").Value.ToString()),
                               name = res.Attribute("name").Value.ToString(),
                               latitude = Double.Parse(res.Element("Latitude").Value, cultureInfo),
                               longitude = Double.Parse(res.Element("Longitude").Value, cultureInfo),
                               address = res.Element("Address").Value.ToString(),
                               photoDesc = res.Element("PhotoDescription").Value.ToString(),
                               photoDir = XmlConvert.DecodeName(res.Element("Photos").Attribute("directory").Value.ToString()),
                               photoFiles = from a in doc.Descendants("Photos")
                                            select new
                                            {
                                                photo = a.Element("PhotoFile").Value.ToString()
                                            },
                               info = res.Element("InformationFile").Value.ToString()
                           };
                foreach (var poi in pois)
                {
                    IEnumerable<string> pF = (poi.photoFiles as IEnumerable<string>);
                    List<string> photoFiles = null;
                    if(pF != null)
                        photoFiles = pF.ToList<string>();
                    AddPushpin(poi.id, poi.name, poi.latitude, poi.longitude, poi.address, poi.photoDesc, poi.photoDir, photoFiles, poi.info);
                };

我不确定 PhotoFiles 的部分,因为当我尝试读取图钉时出现未知的 Object 错误。 这就是我的图钉的样子:

public class MyPushpin : Pushpin
{
    public int ID { get; set; }
    public string Address { get; set; }
    public string PhotoDesc { get; set; }
    public string PhotoDir { get; set; }         
    public List<string> PhotoFiles { get; set; }  

    public MyPushpin() { }

    public MyPushpin(int id, string name, double latitude, double longitude, string address, string photoDesc, string photoDir, List<string> photoFiles, string info)
    {
        Location loc = new Location(latitude, longitude);
        this.ID = id;
        this.Location = loc;
        this.Name = name;
        this.Address = address;
        this.PhotoDesc = photoDesc;
        this.PhotoDir = photoDir;
        this.PhotoFiles = photoFiles;
        this.Tag = info;
    }

    public void Update(string name , string photoDesc, List<string> photoFiles, string info)
    {
        this.Name = name;
        this.PhotoDesc = photoDesc;
        this.PhotoFiles = photoFiles;
        this.Tag = info;
    }

    public override string ToString()
    {
        return String.Format("{0} - {1}", this.ID, this.Location, this.Address, this.PhotoDesc, this.PhotoDir, this.Tag);
    }

这就是我想如何在自定义图钉中使用文件信息的代码:

public partial class Gallery : ChildWindow
{
    List<string> pics = null;
    public Gallery(MyPushpin currentPin)
    {
        InitializeComponent();
        pics = currentPin.PhotoFiles;
        Loaded += (a, b) => {
            LoadImages();
        };
    }

    private void OKButton_Click(object sender, RoutedEventArgs e)
    {
        this.DialogResult = true;
        Close();
    }

    private void CancelButton_Click(object sender, RoutedEventArgs e)
    {
        this.DialogResult = false;
        Close();
    }

    private void LoadImages()
    {
        List<Picture> coll = new List<Picture>();
        pics.ForEach(delegate(String url)
        {
            coll.Add(AddPicture("url"));
        });
        //coll.Add(AddPicture("/pics/pic4poi_9000010-01.jpg"));
        //coll.Add(AddPicture("/pics/pic4poi_9000010-02.jpg"));
        //coll.Add(AddPicture("/pics/pic4poi_9000010-03.jpg"));
        //coll.Add(AddPicture("/pics/pic4poi_9000010-04.jpg"));

        Preview.Source = new BitmapImage(
            new Uri(
                "/pics/pic4poi_9000010-01.jpg",
                UriKind.Relative));
        lbImage.ItemsSource = coll;
    }

    private Picture AddPicture(string path)
    {
        return new Picture
        {
            Href = new BitmapImage(
            new Uri(
                path,
                UriKind.Relative))
        };
    }

    private void lbImage_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        Preview.Source = ((Picture)lbImage.SelectedItem).Href;
    }
}
public class Picture
{
    public ImageSource Href { get; set; }

THX 你的时间洲

您能否更详细地描述您遇到的问题。 有没有调试进去,哪里失败了,有什么异常? 你期望它做什么。

在我的脑海中,这段代码看起来不对:

photoFiles = from a in doc.Descendants("Photos")
                                            select new
                                            {
                                                photo = a.Element("PhotoFile").Value.ToString()
                                            },

将 doc 替换为 res,因为您想要当前元素的子元素,而不是文档的子元素。 您也可以在此处使用 Elements 而不是 Descendants,因为它们是直接子代。 另外,由于有多个照片文件,请尝试 SelectMany(来自 a... 从 b...):

photoFiles = from a in res.Elements("Photos")
             from b in a.Elements("PhotoFile")
                                            select new
                                            {
                                                photo = b.Value.ToString()
                                            },

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM