简体   繁体   中英

Best way to replace XML Text

I have a web service which returns the following XML:

<Validacion>
        <Es_Valido>NK7+22XrSgJout+ZeCq5IA==</Es_Valido>
    </Validacion>
    <Estatus>
        <Estatus>dqrQ7VtQmNFXmXmWlZTL7A==</Estatus>
    </Estatus>
    <Generales>
<Nombre>V4wb2/tq9tEHW80tFkS3knO8i4yTpJzh7Jqi9MxpVVE=</Nombre>
<Apellido>jXyRpjDQvsnzZz+wsq6b42amyTg2np0wckLmQjQx1rCJc8d3dDg6toSdSX200eGi</Apellido>
<Ident_Clie>IYbofEiD+wOCJ+ujYTUxgsWJTnGfVU+jcQyhzgQralM=</Ident_Clie>           <Fec_Creacion>hMI2YyE5h2JVp8CupWfjLy24W7LstxgmlGoDYjPev0r8TUf2Tav9MBmA2Xd9Pe8c</Fec_Creacion>
<Nom_Asoc>CF/KXngDNY+nT99n1ITBJJDb08/wdou3e9znoVaCU3dlTQi/6EmceDHUbvAAvxsKH9MUeLtbCIzqpJq74e  QfpA==</Nom_Asoc>
        <Fec_Defuncion />
    </Generales>

The text inside the tags in encrypted, I need to decrypt the text, I've come up with a regular expressions solution but I don't think it's very optimal, is there a better way to do this? thanks!

I wouldn't use a regular expression. Load the XML with something like LINQ to XML, find every element which just has a text child, and replace the contents of that child with the decrypted form.

Do you know which elements will be encrypted? That would make it even easier. Basically you'll want something along the lines of:

// It's possible that modifying elements while executing Descendants()
// would be okay, but I'm not sure
List<XElement> elements = doc.Descendants().ToList();
foreach (XElement element in elements)
{
    if (ShouldDecrypt(element)) // Whatever this would do
    {
        element.Value = Decrypt(element.Value);
    }
}

(I'm assuming you know how to do the actual decryption part, of course.)

Never ever use regular expressions to parse XML. XmlReader and XmlDocument, both found inside System.Xml, provide a way better way to parse XML.

Do you know the type of encryption used? Look here to get the basics on the Cryptology capabilities in .NET

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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