2

我有一个 xml 字符串,其中包含我想要屏蔽的某些值。我还有一个黑名单列表,其中包含我要屏蔽的元素或属性的名称。如何使用 Linq 做到这一点?

var BlackList=new List<string>{"ssn", "dateofbirth"};

var xml=@"<Rows><Row><SSN>123-12-1234</SSN><Address>123 Somewhere Street</Address><DateOfBirth>12-12-2012</DateOfBirth></Row><Row><SSN value=""123-12-1234""/><Address value=""123 Somewhere Street""/><DateOfBirth value=""12-12-2012""/></Row></Rows>";

结果集如下所示:

"<Rows><Row><SSN>RemovedForSecurity</SSN><Address>123 Somewhere Street</Address><DateOfBirth>RemovedForSecurity</DateOfBirth></Row><Row><SSN value="RemovedForSecurity"/><Address value="123 Somewhere Street"/><DateOfBirth value="RemovedForSecurity"/></Row></Rows>"
4

2 回答 2

5

首先,对所有实际的 XML 使用 LINQ to XML。您以字符串开头并以字符串结尾的事实是偶然的:您实际上是在尝试操作 XML 文档。在这一点上,它相当容易:

var redactedElements = new HashSet<XName>
{
    "SSN",
    "CreditCard"
};
var redactedAttributes = new HashSet<XName>
{
    "dateofbirth",
    ...
};

var elements = doc.Descendants()
                  .Where(x => redactedElements.Contains(x.Name))
                  .ToList();
foreach (var element in elements)
{
    element.Value = "RemovedForSecurity";
}

var attributes = doc.Descendants()
                    .Attributes()
                    .Where(x => redactedAttributes.Contains(x.Name))
                    .ToList();
foreach (var attribute in attributes)
{
    attribute.Value = "RemovedForSecurity";
}

编辑:为了不区分大小写,您将保留一个不区分大小写的本地名称列表:

var redactedElements = new HashSet<string>(StringEqualityComparer.OrdinalIgnoreCase);
{
    "SSN",
    "CreditCard"
};
var elements = doc.Descendants()
                  .Where(x => redactedElements.Contains(x.Name.LocalName))
                  .ToList();
// Ditto for the attributes

不过,如果您指定确切的名称会更好,IMO。

于 2013-07-15T21:31:05.460 回答
1
var unsecureElements = 
    XElement.Parse(xml)
            .Descendants()
            .Where(n => blackList.Contains(n.Name.LocalName.ToLower()));

foreach (var element in unsecureElements)
{
    var valueAttribute = element.Attribute("value");

    if (valueAttribute != null) // check if unsecure element has value attribute
        valueAttribute.SetValue("RemovedForSecurity");
    else
        element.Value = "RemovedForSecurity";
}
于 2013-07-15T21:39:18.647 回答