XElement/LinQ代码失败 [英] XElement/LinQ Code Failure

查看:106
本文介绍了XElement/LinQ代码失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有以下代码:

public static void Replace(filepath)
{
    try
    {
        XElement xD = XElement.Load(filePath);
        foreach (XElement xE in xD.Elements())
        {
            Console.WriteLine(xE.Attribute("attr").Value);
            if (tuv.Attribute("attr") != null)
            {
                Console.WriteLine(xE.Attribute("attr").Value);
                if (Regex.IsMatch(xE.Attribute("attr").EndsWith("AA"))
                {
                    Console.WriteLine("match");
                    tuv.Attribute("attr").Value.Replace("AA", "");
                }
                Console.WriteLine(xE.Attribute("attr").Value);
            }
        }
    }
    catch (Exception e)
    {
        Console.WriteLine("Failure in Replace: {0}", e.ToString());
    }
}

我得到的错误是:替换失败:System.NullReferenceException:对象引用未设置为对象的引用. 在第21行(第一个Console.WriteLine)的Application.Program.Replace(字符串文件路径)处

And the error I'm getting is : Failure in Replace: System.NullReferenceException: Object reference not set to a reference of an object. at Application.Program.Replace(string filepath) in : line 21 (the first Console.WriteLine)

该程序的目的是在XML文件中编辑满足特定条件的任何属性名称.例如,假设我们拥有:

The purpose of this program is to edit any Attribute names in an XML file that meet a certain criteria... So, for instance, say we have:

<element attr="brAA"></element>

这将被编辑为:

<element attr="br"></element>

据我所知,我正在创建一个变量xE,它表示元素xD.Elements()集合的内容.有谁对我为什么会收到此错误有任何见识?

As far as I know, I'm creating a variable xE which represents the contents of the collection of elements xD.Elements()... I've been breaking my head open over this for an hour now! Does anyone have any insight as to why I would be getting this error?

非常感谢!

以下是XML的代码段

Here is a snippet of the XML

<body>
    <par>
    <prop type="Doc">1</prop>
    <elem attr="aaaa">
        <child>REDACTED</child>
    </elem>
    <elem attr="aaAA">
        <child>REDACTED</child>
    </elem>
    <elem lang="abaa">
        <child>REDACTED</child>
    </elem>
    <elem attr="abAA">
        <child>REDACTED</child>
    </elem>
    <elem attr="acaa">
        <child>REDACTED</child>
    </elem>
    </par>
</body>

推荐答案

您正在遍历所有元素,并显示"attr"属性的值.但是某些节点没有"attr"属性,因此会出现错误.删除if块之外的Console.WriteLine,它应该可以工作:

You're iterating through all the elements, and displaying the value of the "attr" attribute. But some nodes don't have the "attr" attribute, hence the error. Remove the Console.WriteLine outside of the if block and it should work:

public static void Replace(filepath)
{
    try
    {
        XElement xD = XElement.Load(filePath);
        foreach (XElement xE in xD.Descendants())
        {
            if (xE.Attribute("attr") != null)
            {
                Console.WriteLine(xE.Attribute("attr").Value);
                if (Regex.IsMatch(xE.Attribute("attr").Value))
                {
                    Console.WriteLine("match");
                    xE.Attribute("attr").Value.Replace("AA", "");
                }
                Console.WriteLine(xE.Attribute("attr").Value);
            }
        }
    }
    catch (Exception e)
    {
        Console.WriteLine("Failure in Replace: {0}", e.ToString());
    }
}

这篇关于XElement/LinQ代码失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆