Click here to Skip to main content
15,878,809 members

[Help] Fix a code about value of element/node in xml file.

Emily Alice asked:

Open original thread
Hi all, a have a 1 file database xml, I add it to my Project Winform C#, now I want when I debug it show a value of element/node bookID had Encryption by method Encryption MD5 or SHA in textbox, Are you understand ? Now here is xml file, picture demo and code, please fix for me, thank very much:
<?xml version="1.0" encoding="utf-8"?>
<data>
    <book>
    <bookID>1111</bookID>
    <bookName>English</bookName>
    <bookPrice>2$</bookPrice>
    </book>
    <book>
    <bookID>222</bookID>
    <bookName>USA</bookName>
    <bookPrice>3$</bookPrice>
    </book>
    <book>
    <bookID>3333</bookID>
    <bookName>Singapore</bookName>
    <bookPrice>4$</bookPrice>
    </book>
</data>


Here is a picture when I debug, it haven't still encryption bookID:

http://i1055.photobucket.com/albums/s505/vn_photo/sss.jpg

Here is picture required I want when debug finish:

http://i1055.photobucket.com/albums/s505/vn_photo/sse.jpg

Here is my code:

C#
private void Form1_Load(object sender, EventArgs e)
       {
           //LoadXml("book.xml");
           XmlDocument xml = new XmlDocument();
           xml.Load("book.xml");
           XmlNodeList xnList = xml.SelectNodes("/data[bookID]");
           foreach (XmlNode xn in xnList)
           {
               XmlNode encryption = xn.SelectSingleNode("bookID");
               if (encryption != null)
               {
                   string pass = encryption["bookID"].InnerText;
                   XmlNodeList CNodes = xn.SelectNodes("book");
                   foreach (XmlNode node in CNodes)
                   {
                       XmlNode encryption1 = node.SelectSingleNode("bookID");
                       if (encryption1 != null)
                       {
                           string md5 = EncrypSHA1(encryption1["bookID"].InnerText);
                       }
                   }
               }
           }
           xml.Save("book.xml");
           LoadXml("book.xml");
       }

       private void LoadXml(string FILE_NAME)
       {
           if (!File.Exists(FILE_NAME))
           {
               MessageBox.Show("No find database!", "Notify!", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
               return;
           }
           StreamReader sr = File.OpenText(FILE_NAME);
           String input;
           input = sr.ReadToEnd();
           sr.Close();
           textBox1.Text = input;
       }

       private static string EncrypSHA1(string sMaHoa)
       {
           UTF32Encoding utf32 = new UTF32Encoding();
           byte[] bytes = utf32.GetBytes(sMaHoa);
           //Encoding.UTF32.GetBytes(
           SHA1 md = new SHA1CryptoServiceProvider();
           MD5 md5 = new MD5CryptoServiceProvider();
           byte[] kq = md.ComputeHash(bytes);
           byte[] kq2 = md5.ComputeHash(kq);
           return Convert.ToBase64String(kq2);
       }
Tags: C# (C# 4.0)

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900