0

所以我有以下代码:

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

我得到的错误是:Replace失败:System.NullReferenceException:对象引用未设置为对象的引用。在 Application.Program.Replace(string filepath) 中:第 21 行(第一个 Console.WriteLine)

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

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

这将被编辑为:

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

据我所知,我正在创建一个变量 xE,它代表元素集合 xD.Elements() 的内容……我已经为此头疼了一个小时!有没有人知道我为什么会收到这个错误?

非常感谢!

这是 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>
4

2 回答 2

1

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

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());
    }
}
于 2012-05-18T15:20:03.223 回答
1

<elem>您只需要处理实际需要替换属性值的元素。您的 .Replace() 没有这样做。

下面的代码遍历<elem>元素,并具有正确的替换:xE.Attribute("attr").Value = xE.Attribute("attr").Value.Replace("AA", "");

我还更改了您的 .EndsWith 以摆脱正则表达式匹配。最后,这里没有针对缺失属性的错误处理。你也应该检查一下。

    foreach (XElement xE in xD.Descendants("elem"))
    {
        //Console.WriteLine(xE.Attribute("attr").Value);
        if (xE.Attribute("attr") != null)
        {
            Console.WriteLine(xE.Attribute("attr").Value);
            if (xE.Attribute("attr").Value.EndsWith("AA"))
            {
                Console.WriteLine("match");
                xE.Attribute("attr").Value = xE.Attribute("attr").Value.Replace("AA", "");
            }
            Console.WriteLine(xE.Attribute("attr").Value);
        }
    }

*编辑- 你问如何写出文件。这将覆盖它。

    using(var sw = new StreamWriter(filepath, false))
    {
        sw.Write(xD.ToString());
        sw.Close();
    }
于 2012-05-18T15:26:14.093 回答