繁体   English   中英

使用C#和XPath从xml清理数据

[英]Scrub data from xml using c# and xpath

我试图在xpaths的帮助下使用c#从xml中删除/清理一些元素。 我试图在两个名为“ Customers”的子标记中将“ social_security_number”的值替换为“ Scrubbed”。 但是我的程序出现了很多错误。 请纠正我。

xml:

<?xml version="1.0" encoding="utf-16"?>
<LoanApplications xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="12345" bundle_id="12225" version="1.0">
    <LoanApplication payment_call="False" version="1.0" app_status="I" perform_dupe_check="1"   bundle_id="12225"  UpdateReviewed="True">
            <Customers id = "12" name =  "krish" ssn = "123456789">
    </LoanApplication>      
    <LoanApplication deal_type="RESPONSE"  payment_call="True" version="1.0"  app_status="I" perform_dupe_check="1"   bundle_id="12225"  UpdateReviewed="True">
    <Customers id = "12" name =  "krish" ssn = "123456789">
    </LoanApplication>
</LoanApplications>

程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {

            XmlDocument doc = new XmlDocument();
            doc.Load("mytestfile.xml");
            doc.SelectSingleNode("/LoanApplications/LoanApplication[@deal_type="%DealTypeALL%"]/LoanApplicationStates/LoanApplicationState/Customers/Customer[@customer_id="%CustIDALL%"]/").Attributes["social_security_number"].InnerText = "Scrubbed";

            doc.Save("mytestfile.xml");

        }
    }
}
var doc = XDocument.Parse(System.IO.File.ReadAllText("C:\\Users\\jason\\Desktop\\Input\\2015\\09\\03\\mytestfile.xml"));
foreach (var customer in doc.Descendants("Customer"))
{
    var ssn = customer.Attribute("social_security_number");
    if (ssn != null)
    {
        ssn.Value = "scrubbed";
    }
}

doc.Save("file.xml");

您需要的节点很少,而不是一个。 因此,您应该使用SelectNodes而不是SelectSingleNode方法。

var doc = new XmlDocument();
doc.Load("mytestfile.xml");

var ssns = doc.SelectNodes("LoanApplications/LoanApplication/LoanApplicationStates/LoanApplicationState/Customers/Customer/@social_security_number");

foreach (XmlAttribute ssn in ssns)
    ssn.InnerText = "Scrubbed";

doc.Save("mytestfile.xml");

您可以对后代使用较短的XPath。 但是它的性能较差。

var ssns = doc.SelectNodes("//Customer/@social_security_number");

使用xml linq真的很容易

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            List<XElement> ss = doc.Descendants().Where(x => x.Attribute("social_security_number") != null).ToList();
            foreach (XElement s in ss)
            {
                s.Attribute("social_security_number").Value = "Scrubbed";
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM