使用 CORPORATE 属性访问命名空间内的 XML

发布于 2025-01-04 14:06:04 字数 2159 浏览 0 评论 0原文

如何使用 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 技术交流群。

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

发布评论

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

评论(1

你的呼吸 2025-01-11 14:06:04

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

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,
            };

Edit:

假设每个 Profile 只有一个 IndividualName 元素,我会像这样重构您的查询:检查空值:

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")
            };

Just use the namespace you already defined:

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,
            };

Edit:

Assuming there is only a single IndividualName element for each Profile I would restructure your query like this to check for null values:

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