如何使用 Linq to XML 中的属性

发布于 2024-12-04 07:17:30 字数 3436 浏览 0 评论 0原文

我想选择如下 XML 元素。请注意 Data 和 Party 是类。有人可以帮助如何实现以下目标:

选择新数据
{
Party.Name = xElem.Element("Name").Value,
Party.PostBox = xElem.Element("PostBox").Value,
}

使用当前代码,我无法访问 Party 的属性。

static void Main(string[] args)
{
     XDocument doc = XDocument.Load(@"c:\test.xml");
     var q = from xElem in doc.Descendants("Party")
         where (int)xElem.Attribute("ID") == 1
         select new Data
         {

         };
}

public class Data
{
    public Party Party { get; set; }
    public Data()
    {
        this.Party = new Party();
    }
}

public class Party
{
    string name;
    string postbox;

    public string Name
    {
        get { return name; }
        set { this.name = value; }
    }

    public string PostBox
    {
        get { return postbox; }
        set { this.postbox = value; }
    }
}

@Jon Skeet:以下是示例代码。我仅在运行时收到“对象引用未设置到对象实例”错误。

static void Main(string[] args)
{
    XDocument doc = XDocument.Load(@"c:\test\data.xml");
    var props = from xElem in doc.Descendants("Party")
        where (int)xElem.Attribute("ID") == 1
        select new Data
        {
              Party =
              {
                  Name = xElem.Element("Name").Value.ToString(),
                  PostBox = xElem.Element("PostBox").Value.ToString(),
                  Tax =
                  {
                     CompanyID = xElem.Element("Tax").Element("CompanyID").Value.ToString()
                  }
              }
        }
}


public class Data
{
    public Party Party { get; set; }
    public Data()
    {
        this.Party= new Party();
    }
}

public class Party
{
    string name;
    string postbox;

    public Tax Tax { get; set; }

    public string Name
    {
        get { return name; }
        set { this.name = value; }
    }

    public string PostBox
    {
        get { return postbox; }
        set { this.postbox = value; }
    }
}

public class Tax
{
    string companyid;

    public string CompanyID
    {
        get { return companyid; }
        set { this.companyid = value; }
    }
}

@Jon Skeet:以下是示例代码。我仅在运行时收到“对象引用未设置到对象实例”错误。

static void Main(string[] args)
{
    XDocument doc = XDocument.Load(@"c:\test\data.xml");
    var props = from xElem in doc.Descendants("Party")
        where (int)xElem.Attribute("ID") == 1
        select new Data
        {
              Party =
              {
                  Name = xElem.Element("Name").Value.ToString(),
                  PostBox = xElem.Element("PostBox").Value.ToString(),
                  Tax =
                  {
                     CompanyID = xElem.Element("Tax").Element("CompanyID").Value.ToString()
                  }
              }
        }
}


public class Data
{
    public Party Party { get; set; }
    public Data()
    {
        this.Party= new Party();
    }
}

public class Party
{
    string name;
    string postbox;

    public Tax Tax { get; set; }

    public string Name
    {
        get { return name; }
        set { this.name = value; }
    }

    public string PostBox
    {
        get { return postbox; }
        set { this.postbox = value; }
    }
}

public class Tax
{
    string companyid;

    public string CompanyID
    {
        get { return companyid; }
        set { this.companyid = value; }
    }
}

I want to select XML elements as following. Please know Data and Party are classes. Can someone help how to achieve following:

select new Data
{
Party.Name = xElem.Element("Name").Value,
Party.PostBox = xElem.Element("PostBox").Value,
}

With current code, I cannot access properties of Party.

static void Main(string[] args)
{
     XDocument doc = XDocument.Load(@"c:\test.xml");
     var q = from xElem in doc.Descendants("Party")
         where (int)xElem.Attribute("ID") == 1
         select new Data
         {

         };
}

public class Data
{
    public Party Party { get; set; }
    public Data()
    {
        this.Party = new Party();
    }
}

public class Party
{
    string name;
    string postbox;

    public string Name
    {
        get { return name; }
        set { this.name = value; }
    }

    public string PostBox
    {
        get { return postbox; }
        set { this.postbox = value; }
    }
}

@Jon Skeet: Following is the sample code. I get "Object reference not set to an instance of an object" error at runtime only.

static void Main(string[] args)
{
    XDocument doc = XDocument.Load(@"c:\test\data.xml");
    var props = from xElem in doc.Descendants("Party")
        where (int)xElem.Attribute("ID") == 1
        select new Data
        {
              Party =
              {
                  Name = xElem.Element("Name").Value.ToString(),
                  PostBox = xElem.Element("PostBox").Value.ToString(),
                  Tax =
                  {
                     CompanyID = xElem.Element("Tax").Element("CompanyID").Value.ToString()
                  }
              }
        }
}


public class Data
{
    public Party Party { get; set; }
    public Data()
    {
        this.Party= new Party();
    }
}

public class Party
{
    string name;
    string postbox;

    public Tax Tax { get; set; }

    public string Name
    {
        get { return name; }
        set { this.name = value; }
    }

    public string PostBox
    {
        get { return postbox; }
        set { this.postbox = value; }
    }
}

public class Tax
{
    string companyid;

    public string CompanyID
    {
        get { return companyid; }
        set { this.companyid = value; }
    }
}

@Jon Skeet: Following is the sample code. I get "Object reference not set to an instance of an object" error at runtime only.

static void Main(string[] args)
{
    XDocument doc = XDocument.Load(@"c:\test\data.xml");
    var props = from xElem in doc.Descendants("Party")
        where (int)xElem.Attribute("ID") == 1
        select new Data
        {
              Party =
              {
                  Name = xElem.Element("Name").Value.ToString(),
                  PostBox = xElem.Element("PostBox").Value.ToString(),
                  Tax =
                  {
                     CompanyID = xElem.Element("Tax").Element("CompanyID").Value.ToString()
                  }
              }
        }
}


public class Data
{
    public Party Party { get; set; }
    public Data()
    {
        this.Party= new Party();
    }
}

public class Party
{
    string name;
    string postbox;

    public Tax Tax { get; set; }

    public string Name
    {
        get { return name; }
        set { this.name = value; }
    }

    public string PostBox
    {
        get { return postbox; }
        set { this.postbox = value; }
    }
}

public class Tax
{
    string companyid;

    public string CompanyID
    {
        get { return companyid; }
        set { this.companyid = value; }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秉烛思 2024-12-11 07:17:30

您想要:

// Modifies the existing Party created in the Data constructor
select new Data 
{ 
    Party =
    {
        Name = xElem.Element("Name").Value, 
        PostBox = xElem.Element("PostBox").Value
    }
}

或:

// Creates a new Party and then calls the Data.Party setter
select new Data 
{ 
    Party = new Party
    {
        Name = xElem.Element("Name").Value, 
        PostBox = xElem.Element("PostBox").Value
    }
}

请注意,这与 XML 无关,与 LINQ 也没有任何关系 - 它只是使用对象初始值设定项功能。

您可能需要考虑的一件事是使用从 XElementstring 的显式转换,而不是使用 Value - 这样如果元素丢失,你得到一个空引用而不是异常。这取决于您想要什么行为,但作为一种选择值得了解。

You want:

// Modifies the existing Party created in the Data constructor
select new Data 
{ 
    Party =
    {
        Name = xElem.Element("Name").Value, 
        PostBox = xElem.Element("PostBox").Value
    }
}

or:

// Creates a new Party and then calls the Data.Party setter
select new Data 
{ 
    Party = new Party
    {
        Name = xElem.Element("Name").Value, 
        PostBox = xElem.Element("PostBox").Value
    }
}

Note that this has nothing to do with XML and nothing really to do with LINQ - it's just using object initializer features.

One thing you may want to consider is using the explicit conversion from XElement to string instead of using Value - that way if an element is missing, you get a null reference instead of an exception. It depends on what behaviour you want, but it's worth knowing about as an option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文