比较XML文件上的一些数据 [英] Comparing some data on an XML file

查看:104
本文介绍了比较XML文件上的一些数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我不久前发了一条帖子.

我决定不使用我的主程序,而是决定使用一个较小的程序进行测试.

我有atm的问题是,如果我使用文件sig并将其硬编码在我的代码中..它可以工作...但是如果我告诉它使用我的字符串值...它不喜欢它...即使该值完全相同...

我将发布我的代码,然后在发布结束时进行一些解释:

我的XML文件:

Hi guys,

I made a post not long ago.

Instead of working on my main program i decided to work on a smaller program for testing.

my problem i am having atm, is if i use a file sig i put hard coded in my code.. it works... but if i tell it to use my string value... it doesnt like it... even though the value is exactly the same...

I will post my code, and then explain a little more at the end of the post:

My XML File:

<?xml version="1.0"?>
<Filesigs>
  <Images>
    <sig name = "jpg">FF-0D-FF-E0</sig>
    <sig name = "jpg">FF-D8-FF-E8</sig>
    <sig name = "png">89-50-4E-47-0D-0A-1A-0A</sig>
    <sig name = "gif">47-49-46-38-37-61</sig>
    <sig name = "gif">47-49-46-38-39-61</sig>
    <sig name = "tiff">49-20-49</sig>
    <sig name = "tiff">49-49-2A-00</sig>
    <sig name = "bmp">42-4D</sig>
  </Images>
</Filesigs>



我的程式码:



my program code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml.Linq;

namespace WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            // open file
            FileStream fs = new FileStream(@"C:\Users\Alan\Desktop\Koala.bob", FileMode.Open);
      
            // allocate buffer for whole file
            byte[] buffer = new byte[20];
            fs.Read(buffer, 0, 20);
            string hex = BitConverter.ToString(buffer);
            textBoxStoredExtension.Text = (Path.GetExtension(@"C:\Users\Alan\Desktop\Koala.bob"));
            string storedFileSig = (hex);
            //string storedFileSig = ("FF-0D-FF-E0");
            textBoxStored.Text = hex;
            XDocument myDoc = XDocument.Load("FileSigs.xml");
            IEnumerable<String> result = from FileSigs in myDoc.Elements()
                                         from Images in FileSigs.Elements()
                                         from sigs in Images.Elements()
                                         where (storedFileSig.Contains(sigs.Value))
                                         select sigs.Attribute("name").Value;
            foreach (String r in result)
            {
                textBoxXMLExtension.Text = r;
            }
            //Console.ReadLine();
        }
    }
}



好,

如您所见,我的程序占用文件的前20个字节.

转换为十六进制并存储在名为"hex"的字符串中

然后,它获取文件的文件扩展名,并立即在文本框中显示它. (稍后将使用,但目前不重要)

然后转到XML文件并检查XML文件,直到找到十六进制"包含的值.

所以...当我正在测试...我正在使用一个"image.bob"文件...当我读入它时..它返回一个20字节的十六进制值,每个字节之间带有- br/>
所以当我运行它时:我得到了FF-D8-FF-E0-00-10-4A-46-49-46-00-01-01-01-00-60-00-60-00-00来自"image.bob"文件的20个字节.

然后应检查XML,并查看我的XML文件中的前1个是否匹配,因为20个字节包含"FF-D8-FF-E0".

因为它匹配.然后,它应该显示.jpg(取自XML文件),并显示在从头开始显示".bob"文件扩展名的文本框下方的文本框中. (当我使用String Hex时,这个WONT起作用了……但是,从我的代码中可以看到,我已经注释掉了//string storageFileSig =("FF-0D-FF-E0"); ....的确起作用. .

然后我希望它在表单上的listBoxImages中显示文件名"image.bob".


但我似乎无法使它起作用:(

有人可以在这里帮我吗?

非常感谢,
Hypeh



Ok,

as you can see, my program takes the first 20 bytes of the file.

converts to hex and stores in a string called "hex"

it then gets the file extension of the file and displays it in a text box for now. (will be used further later but not important at the moment)

it then goes to the XML file and checks through the XML file until it finds a value that the "hex" contains.

so... when i am testing... i am using a "image.bob" file... when i read it in.. it returns a 20 byte hex calue with -''s between each byte...

so when i run it: i am given FF-D8-FF-E0-00-10-4A-46-49-46-00-01-01-01-00-60-00-60-00-00 for the 20 bytes from the "image.bob" file.

it then should check the XML and see that the first 1 in my XML file matches as the 20 bytes contains "FF-D8-FF-E0".

because it matches. it should then display .jpg (taken from the XML file) and display it in a text box below the text box that displays the ".bob" file extension from the start. (this WONT work when i use String Hex... but as you can see from my code i have commented out //string storedFileSig = ("FF-0D-FF-E0");.... which does work...

then I want it to display the file name "image.bob" into the listBoxImages that I have on the form.


but i cant seem to get it to work :(

can anyone help me here?

many thanks,
Hypeh

推荐答案

您有这两行:
You have these two lines:
<sig name = "jpg">FF-0D-FF-E0</sig>    
<sig name = "jpg">FF-D8-FF-E8</sig>


您的文件image.bob开头为:
FF-D8-FF-E0
您没有得到任何结果,因为没有人匹配-这是两者的混合体.
如果您仔细看一下,发现样本有所不同:
FF-0D-FF-E0
并匹配第一行.
所以一切的行为都像预期的那样.
我假设您需要将FF-D8-FF-E0添加到XML文件中.


Your file image.bob starts with:
FF-D8-FF-E0
You don''t get any result because neither one matches -its a mixture of both.
If you look carefully than your sample is different:
FF-0D-FF-E0
and matches the first line.
So everything behaves like expected.

I assume you need to add FF-D8-FF-E0 to your XML file.


这篇关于比较XML文件上的一些数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆