创建数据类型列表的属性 [英] creating properties of data type List

查看:75
本文介绍了创建数据类型列表的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串格式的XML文件. XML文件有时可能包含多个相同标签之一,就像它可能具有多个msisdn标签一样.我正在使用linq to XML来检索所有msisdn的标签值.

我有一个具有以下属性的类:

I have an XML file in string format. The XML file may sometimes contain more that one of the same tags, like it may have multiple msisdn tags. I am using linq to XML to retrieve all of the msisdn''s tags values.

I have a class with the property as follow:

private List<int> _msisdn;
public List<int> msisdn
{
   get { return _msisdn; }
   set { _msisdn = value; }
}


当我使用Linq to XML查询时,出现错误消息:


when I use a Linq to XML query, I get the error saying:

Object reference not set to an instance of an object.



这是linq代码:



Here is the linq code:

public void getMsidn(string xml)
        {
            XElement xDoc = XElement.Parse(xml);

            IEnumerable<XElement> da = from p in xDoc.Descendants("msisdn")
                                       select p;

            foreach (XElement x in da)
            {
                _msisdn.Add((int)x);
            }
        }



我希望能够通过使用此查询,将从xml检索到的所有值添加到上述属性.

有关此的任何帮助吗?



I want to be able by using this query, add all the values retrieved from the xml to the property mentioned above.

Any help regarding this?

推荐答案

尝试这个

Try this one

public void getMsidn(string xml)
        {
            XElement xDoc = XElement.Parse(xml);
 
            IEnumerable<xelement> da = from p in xDoc.Descendants("msisdn")
                                       select p;
 
            foreach (XElement x in da)
            {
                msisdn.Add((int)x);
            }
        }


我自己使用以下方法找到了解决方案:

我的财产:

I have found a solution myself, using the following approach:

my Property:

private IEnumerable<int> _msisdn;
        public IEnumerable<int> msisdn
        {
            get { return _msisdn; }
            set { _msisdn = value; }
        }



而我的linq to xml查询:



And my linq to xml query:

public void getMsidn(string xml)
        {
            XElement xDoc = XElement.Parse(xml);

            _msisdn = from p in xDoc.Descendants("msisdn")
                                       select (int)p;
        }



这完全可以满足我的需求.

感谢您的帮助.



This works perfectly for what I need it.

Thanks for the help.


这篇关于创建数据类型列表的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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