首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用属性CORPORATE在命名空间中访问XML

使用属性CORPORATE在命名空间中访问XML
EN

Stack Overflow用户
提问于 2012-02-10 16:43:27
回答 1查看 112关注 0票数 0

如何使用XML名称访问XML元素nameSur (这是我想要的,只是公司名称)。我一直将对象引用设置为对象的实例。在from children in lv1.Element("IndividualName").Descendants()线上。如有任何建议,将不胜感激。

代码语言:javascript
复制
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

代码语言:javascript
复制
     <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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-02-10 16:51:08

只需使用您已经定义的名称空间:

代码语言:javascript
复制
var lv1s = from lv1 in xmlDoc.Descendants(aw + "Profile")
            where lv1.Attribute("profileType").Value.Equals(theme)
            //where lv1.Element("IndividualName").Value == "IndividualName"
            from child in lv1.Element(aw + "IndividualName").Descendants()
            select new
            {
                Header = lv1.Attribute("profileType").Value,
                elemento = child.Value,
            };

编辑:

假设每个IndividualName只有一个Profile元素,我将对查询进行如下重构,以检查null值:

代码语言:javascript
复制
var lv1s = from lv1 in xmlDoc.Descendants(aw + "Profile")
           where lv1.Attribute("profileType").Value.Equals(theme) && lv1.Element(aw + "IndividualName") != null
            select new 
            {
                Header = lv1.Attribute("profileType").Value,
                elemento = (string)lv1.Element(aw + "IndividualName").Element(aw+"nameSur")
            };
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9231380

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档