如何使用 Linq to XML 中的属性
我想选择如下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想要:
或:
请注意,这与 XML 无关,与 LINQ 也没有任何关系 - 它只是使用对象初始值设定项功能。
您可能需要考虑的一件事是使用从
XElement
到string
的显式转换,而不是使用Value
- 这样如果元素丢失,你得到一个空引用而不是异常。这取决于您想要什么行为,但作为一种选择值得了解。You want:
or:
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
tostring
instead of usingValue
- 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.