ryudice
ryudice

Reputation: 37366

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!

Upvotes: 1

Views: 438

Answers (3)

Matti Virkkunen
Matti Virkkunen

Reputation: 65116

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

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1499770

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.)

Upvotes: 6

Ian Jacobs
Ian Jacobs

Reputation: 5501

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

Upvotes: 1

Related Questions