【问题标题】:Silverlight 4.0 reading complex xml with linqSilverlight 4.0 使用 linq 读取复杂的 xml
【发布时间】:2011-06-08 00:40:04
【问题描述】:

我遇到了以下 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 的部分,因为当我尝试读取图钉时出现未知对象错误。 这是我的图钉的样子:

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

感谢您的时间 洲

【问题讨论】:

    标签: c# xml linq silverlight-4.0


    【解决方案1】:

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

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

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

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

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

    【讨论】:

    • 嗨,我找到了错误。这是我尝试从 XML 检索 PhotoFiles 的部分。我已经尝试了您的建议,但返回的 List 仍然是空的。
    • 我已将查询修改为:photoFiles = res.Elements("Photos") .Select(x => x.Value) .ToList(),根据此线程:link现在我得到了 PhotoFile-Strings 但未分离。第一个 List 元素的内容如下所示: pic4poi_9000010-01.jpgpic4poi_9000010-02.jpgpic4poi_9000010-03.jpgpic4poi_9000010-04.jpg 如何将它们分开?
    • 我的 linq 查询有错误 photoFiles = res.Elements("Photos") .Select(x => x.Value) .ToList() 它必须是这样才能工作: photoFiles = res.Descendants( "PhotoFile") .Select(x => x.Value) .ToList() Cheers Chau
    猜你喜欢
    • 1970-01-01
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    • 2018-05-21
    • 1970-01-01
    相关资源
    最近更新 更多