使用 CORPORATE 属性访问命名空间内的 XML
如何使用 Value=Company Name 访问 XML 元素 nameSur (这就是我想要的,只是 [公司名称])。我不断在 from Children in lv1.Element("IndividualName").Descendants() 行中收到未设置为对象实例的对象引用。。任何建议将不胜感激。
static public string GetAttributeAgencyAction2(string parFileName)
{
string retVal = string.Empty;
XNamespace aw = "profile.information.4.0";
XDocument xmlDoc = null;
using (StreamReader oReader = new StreamReader(parFileName, Encoding.GetEncoding("ISO-8859-1")))
{
xmlDoc = XDocument.Load(oReader);
}
var theme = "CORPORATE";
var lv1s = from lv1 in xmlDoc.Descendants(aw + "Profile")
where lv1.Attribute("profileType").Value.Equals(theme)
//where lv1.Element("IndividualName").Value == "IndividualName"
from children in lv1.Element("IndividualName").Descendants()
select new
{
Header = lv1.Attribute("profileType").Value,
elemento = children.Element("IndividualName").Value,
};
//Loop through results
foreach (var lv1 in lv1s)
{
Console.WriteLine(lv1.Header + " "+lv1.elemento);
retVal = lv1.Header;
}
return retVal;
}
这是我的 XML
<Profile xmlns="profile.information.4.0" profileType="CORPORATE" gender="UNKNOWN">
<creatorCode>DELPHI</creatorCode>
<createdDate>2012-01-24T19:35:10.000</createdDate>
<lastUpdaterCode>DELPHI</lastUpdaterCode>
<lastUpdated>2012-01-30T12:17:01.000</lastUpdated>
<genericName>Bonafont</genericName>
<IndividualName>
<nameSur>Company Name</nameSur>
</IndividualName>
<mfResort>5924</mfResort>
<mfResortProfileID>1249468</mfResortProfileID>
<mfContactLast>Ernert</mfContactLast>
<mfContactFirst>Ramillo</mfContactFirst>
<mfAllowMail>NO</mfAllowMail>
<mfAllowEMail>NO</mfAllowEMail>
<mfGuestPriv>NO</mfGuestPriv>
</Profile>
How can I access the XML element nameSur with the Value=Company Name(This is what I want back, just [Company Name]). I keep getting Object reference not set to an instance of an object. in the from children in lv1.Element("IndividualName").Descendants()
line. Any advise will be appreciated.
static public string GetAttributeAgencyAction2(string parFileName)
{
string retVal = string.Empty;
XNamespace aw = "profile.information.4.0";
XDocument xmlDoc = null;
using (StreamReader oReader = new StreamReader(parFileName, Encoding.GetEncoding("ISO-8859-1")))
{
xmlDoc = XDocument.Load(oReader);
}
var theme = "CORPORATE";
var lv1s = from lv1 in xmlDoc.Descendants(aw + "Profile")
where lv1.Attribute("profileType").Value.Equals(theme)
//where lv1.Element("IndividualName").Value == "IndividualName"
from children in lv1.Element("IndividualName").Descendants()
select new
{
Header = lv1.Attribute("profileType").Value,
elemento = children.Element("IndividualName").Value,
};
//Loop through results
foreach (var lv1 in lv1s)
{
Console.WriteLine(lv1.Header + " "+lv1.elemento);
retVal = lv1.Header;
}
return retVal;
}
Here is my XML
<Profile xmlns="profile.information.4.0" profileType="CORPORATE" gender="UNKNOWN">
<creatorCode>DELPHI</creatorCode>
<createdDate>2012-01-24T19:35:10.000</createdDate>
<lastUpdaterCode>DELPHI</lastUpdaterCode>
<lastUpdated>2012-01-30T12:17:01.000</lastUpdated>
<genericName>Bonafont</genericName>
<IndividualName>
<nameSur>Company Name</nameSur>
</IndividualName>
<mfResort>5924</mfResort>
<mfResortProfileID>1249468</mfResortProfileID>
<mfContactLast>Ernert</mfContactLast>
<mfContactFirst>Ramillo</mfContactFirst>
<mfAllowMail>NO</mfAllowMail>
<mfAllowEMail>NO</mfAllowEMail>
<mfGuestPriv>NO</mfGuestPriv>
</Profile>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需使用您已经定义的命名空间:
Edit:
假设每个
Profile
只有一个IndividualName
元素,我会像这样重构您的查询:检查空值:Just use the namespace you already defined:
Edit:
Assuming there is only a single
IndividualName
element for eachProfile
I would restructure your query like this to check for null values: