1.菜鸟怎么样学习C语言
2.des算法源代码
3.Ubuntu 9.10ä¸å®è£
å使ç¨ffmpegç详ç»è¿ç¨ï¼
4.C#用datagriview控件增加删除行数据,源码并且更新到xml中,源码求代码(急)如图
菜鸟怎么样学习C语言
1、源码学习C语言,源码要从入门到精通,源码需要读哪些书(从简单的源码大牛买卖点源码到难的排序,越详细越好,源码最好都能注释下选择这本书的源码理由)?
入门阶段:还是老谭那本。
理由:虽然不能说它写得有多好,源码但是源码你现在要做的是入门,要快速的源码掌握c的基本语法,这本书很好理解,源码能够让你在最短的源码时间内大致掌握这门语言的概更。
第二阶段:《c程序设计语言》(The源码 C Programming Language)和《C语言解惑》(The C puzzle book)
理由:《the c programming language》号称c语言圣经。其实它也只是源码一本介绍基础语法的书,不做入门教程是因为对于初学者来说,它难度稍大,强势出击源码分享之所以推荐,是因为它能让你---系统而严密的---把C语言知识构架整理一遍。《c语言解惑》,系统的整理了c语法中容易让你产生迷惑或容易犯错的地方(如a+++++b等),这时候你才算真正开始学习c语言了.(以上两本现在出重印了,应该在书店可以买到)
第三阶段:《C陷阱与缺陷》(C Traps and Pitfalls)和《高质量c/c++编程指南》
理由:《c陷阱与缺陷》是让对c的理解有质变得一本书,如如何理解(*(void(*)())0)()等问题,我的感觉是看完这本书让我真正从小菜鸟变成了老菜鸟。《高质量...》,庄家建仓精准源码终于有一本国产的了,呵呵,我认为这本书是把你从土匪变成正规军的最好指南,该书涉及编程风格、效率、重载、健壮性等一些列之前很难注意的问题。(以上两本《c陷阱...》已绝版,recv源码怎么用不过网上可以下载到中英文版,《高质量...》本身就是网络书,很容易找到)
其它推荐书:《c专家编程》《c和指针》
说实话这两本书我并没有看过(或看完),但有口皆碑,都是经典之作,不过都已经绝版,仔细找找网上有下载的。
2、ucostask.c源码有哪些好的C语言练习(越难的越好)?
对语言本身的练习其实就是你对它的理解,用得多了,注意得多了,自然就ok了。其它思维上的练习主要是算法和数据结构方面的,严老的《数据结构题集(C语言版)》如果你能做完就相当了不起了。
3、要学精C语言,还要具备哪些条件?
其实c语言也只是一个工具而已,就像锄头一样,你每天挖地锄田自然就精通它了。只要不是白痴,应该都没有问题。
4、C语言学完后接下去要学哪些语言好?
这个我就没有资格回答了,因为除了少量汇编和c++,我大部分只用到了c,不过也许是一脉相承的c++吧。c是面向过程的语言,学习c++面向对象的思想。
5、各位网友如果在学习计算机语言方面还有一些独到的见解,欢迎提出!
不是我提出的,不过真的很经典——“天下程序一大抄”,呵呵。
des算法源代码
des.h文件:
#ifndef CRYPTOPP_DES_H
#define CRYPTOPP_DES_H
#include "cryptlib.h"
#include "misc.h"
NAMESPACE_BEGIN(CryptoPP)
class DES : public BlockTransformation
{
public:
DES(const byte *userKey, CipherDir);
void ProcessBlock(const byte *inBlock, byte * outBlock) const;
void ProcessBlock(byte * inoutBlock) const
{ DES::ProcessBlock(inoutBlock, inoutBlock);}
enum { KEYLENGTH=8, BLOCKSIZE=8};
unsigned int BlockSize() const { return BLOCKSIZE;}
protected:
static const word Spbox[8][];
SecBlock<word> k;
};
class DESEncryption : public DES
{
public:
DESEncryption(const byte * userKey)
: DES (userKey, ENCRYPTION) { }
};
class DESDecryption : public DES
{
public:
DESDecryption(const byte * userKey)
: DES (userKey, DECRYPTION) { }
};
class DES_EDE_Encryption : public BlockTransformation
{
public:
DES_EDE_Encryption(const byte * userKey)
: e(userKey, ENCRYPTION), d(userKey + DES::KEYLENGTH, DECRYPTION) { }
void ProcessBlock(const byte *inBlock, byte * outBlock) const;
void ProcessBlock(byte * inoutBlock) const;
enum { KEYLENGTH=, BLOCKSIZE=8};
unsigned int BlockSize() const { return BLOCKSIZE;}
private:
DES e, d;
};
class DES_EDE_Decryption : public BlockTransformation
{
public:
DES_EDE_Decryption(const byte * userKey)
: d(userKey, DECRYPTION), e(userKey + DES::KEYLENGTH, ENCRYPTION) { }
void ProcessBlock(const byte *inBlock, byte * outBlock) const;
void ProcessBlock(byte * inoutBlock) const;
enum { KEYLENGTH=, BLOCKSIZE=8};
unsigned int BlockSize() const { return BLOCKSIZE;}
private:
DES d, e;
};
class TripleDES_Encryption : public BlockTransformation
{
public:
TripleDES_Encryption(const byte * userKey)
: e1(userKey, ENCRYPTION), d(userKey + DES::KEYLENGTH, DECRYPTION),
e2(userKey + 2*DES::KEYLENGTH, ENCRYPTION) { }
void ProcessBlock(const byte *inBlock, byte * outBlock) const;
void ProcessBlock(byte * inoutBlock) const;
enum { KEYLENGTH=, BLOCKSIZE=8};
unsigned int BlockSize() const { return BLOCKSIZE;}
private:
DES e1, d, e2;
};
class TripleDES_Decryption : public BlockTransformation
{
public:
TripleDES_Decryption(const byte * userKey)
: d1(userKey + 2*DES::KEYLENGTH, DECRYPTION), e(userKey + DES::KEYLENGTH, ENCRYPTION),
d2(userKey, DECRYPTION) { }
void ProcessBlock(const byte *inBlock, byte * outBlock) const;
void ProcessBlock(byte * inoutBlock) const;
enum { KEYLENGTH=, BLOCKSIZE=8};
unsigned int BlockSize() const { return BLOCKSIZE;}
private:
DES d1, e, d2;
};
NAMESPACE_END
#endif
des.cpp文件:
// des.cpp - modified by Wei Dai from:
/*
* This is a major rewrite of my old public domain DES code written
* circa , which in turn borrowed heavily from Jim Gillogly's
* public domain code. I pretty much kept my key scheduling code, but
* the actual encrypt/decrypt routines are taken from from Richard
* Outerbridge's DES code as printed in Schneier's "Applied Cryptography."
*
* This code is in the public domain. I would appreciate bug reports and
* enhancements.
*
* Phil Karn KA9Q, karn@unix.ka9q.ampr.org, August .
*/
#include "pch.h"
#include "misc.h"
#include "des.h"
NAMESPACE_BEGIN(CryptoPP)
/* Tables defined in the Data Encryption Standard documents
* Three of these tables, the initial permutation, the final
* permutation and the expansion operator, are regular enough that
* for speed, we hard-code them. They're here for reference only.
* Also, the S and P boxes are used by a separate program, gensp.c,
* to build the combined SP box, Spbox[]. They're also here just
* for reference.
*/
#ifdef notdef
/* initial permutation IP */
static byte ip[] = {
, , , , , , , 2,
, , , , , , , 4,
, , , , , , , 6,
, , , , , , , 8,
, , , , , , 9, 1,
, , , , , , , 3,
, , , , , , , 5,
, , , , , , , 7
};
/* final permutation IP^-1 */
static byte fp[] = {
, 8, , , , , , ,
, 7, , , , , , ,
, 6, , , , , , ,
, 5, , , , , , ,
, 4, , , , , , ,
, 3, , , , , , ,
, 2, , , , , , ,
, 1, , 9, , , ,
};
/* expansion operation matrix */
static byte ei[] = {
, 1, 2, 3, 4, 5,
4, 5, 6, 7, 8, 9,
8, 9, , , , ,
, , , , , ,
, , , , , ,
, , , , , ,
, , , , , ,
, , , , , 1
};
/* The (in)famous S-boxes */
static byte sbox[8][] = {
/* S1 */
, 4, , 1, 2, , , 8, 3, , 6, , 5, 9, 0, 7,
0, , 7, 4, , 2, , 1, , 6, , , 9, 5, 3, 8,
4, 1, , 8, , 6, 2, , , , 9, 7, 3, , 5, 0,
, , 8, 2, 4, 9, 1, 7, 5, , 3, , , 0, 6, ,
/* S2 */
, 1, 8, , 6, , 3, 4, 9, 7, 2, , , 0, 5, ,
3, , 4, 7, , 2, 8, , , 0, 1, , 6, 9, , 5,
0, , 7, , , 4, , 1, 5, 8, , 6, 9, 3, 2, ,
, 8, , 1, 3, , 4, 2, , 6, 7, , 0, 5, , 9,
/* S3 */
, 0, 9, , 6, 3, , 5, 1, , , 7, , 4, 2, 8,
, 7, 0, 9, 3, 4, 6, , 2, 8, 5, , , , , 1,
, 6, 4, 9, 8, , 3, 0, , 1, 2, , 5, , , 7,
1, , , 0, 6, 9, 8, 7, 4, , , 3, , 5, 2, ,
/* S4 */
7, , , 3, 0, 6, 9, , 1, 2, 8, 5, , , 4, ,
, 8, , 5, 6, , 0, 3, 4, 7, 2, , 1, , , 9,
, 6, 9, 0, , , 7, , , 1, 3, , 5, 2, 8, 4,
3, , 0, 6, , 1, , 8, 9, 4, 5, , , 7, 2, ,
/* S5 */
2, , 4, 1, 7, , , 6, 8, 5, 3, , , 0, , 9,
, , 2, , 4, 7, , 1, 5, 0, , , 3, 9, 8, 6,
4, 2, 1, , , , 7, 8, , 9, , 5, 6, 3, 0, ,
, 8, , 7, 1, , 2, , 6, , 0, 9, , 4, 5, 3,
/* S6 */
, 1, , , 9, 2, 6, 8, 0, , 3, 4, , 7, 5, ,
, , 4, 2, 7, , 9, 5, 6, 1, , , 0, , 3, 8,
9, , , 5, 2, 8, , 3, 7, 0, 4, , 1, , , 6,
4, 3, 2, , 9, 5, , , , , 1, 7, 6, 0, 8, ,
/* S7 */
4, , 2, , , 0, 8, , 3, , 9, 7, 5, , 6, 1,
, 0, , 7, 4, 9, 1, , , 3, 5, , 2, , 8, 6,
1, 4, , , , 3, 7, , , , 6, 8, 0, 5, 9, 2,
6, , , 8, 1, 4, , 7, 9, 5, 0, , , 2, 3, ,
/* S8 */
, 2, 8, 4, 6, , , 1, , 9, 3, , 5, 0, , 7,
1, , , 8, , 3, 7, 4, , 5, 6, , 0, , 9, 2,
7, , 4, 1, 9, , , 2, 0, 6, , , , 3, 5, 8,
2, 1, , 7, 4, , 8, , , , 9, 0, 3, 5, 6,
};
/* -bit permutation function P used on the output of the S-boxes */
static byte pi[] = {
, 7, , ,
, , , ,
1, , , ,
5, , , ,
2, 8, , ,
, , 3, 9,
, , , 6,
, , 4,
};
#endif
/* permuted choice table (key) */
static const byte pc1[] = {
, , , , , , 9,
1, , , , , , ,
, 2, , , , , ,
, , 3, , , , ,
, , , , , , ,
7, , , , , , ,
, 6, , , , , ,
, , 5, , , , 4
};
/* number left rotations of pc1 */
static const byte totrot[] = {
1,2,4,6,8,,,,,,,,,,,
};
/* permuted choice key (table) */
static const byte pc2[] = {
, , , , 1, 5,
3, , , 6, , ,
, , , 4, , 8,
, 7, , , , 2,
, , , , , ,
, , , , , ,
, , , , , ,
, , , , ,
};
/* End of DES-defined tables */
/* bit 0 is left-most in byte */
static const int bytebit[] = {
,,,,,,,
};
/* Set key (initialize key schedule array) */
DES::DES(const byte *key, CipherDir dir)
: k()
{
SecByteBlock buffer(++8);
byte *const pc1m=buffer; /* place to modify pc1 into */
byte *const pcr=pc1m+; /* place to rotate pc1 into */
byte *const ks=pcr+;
register int i,j,l;
int m;
for (j=0; j<; j++) { /* convert pc1 to bits of key */
l=pc1[j]-1; /* integer bit location */
m = l & ; /* find bit */
pc1m[j]=(key[l>>3] & /* find which key byte l is in */
bytebit[m]) /* and which bit of that byte */
1 : 0; /* and store 1-bit result */}
for (i=0; i<; i++) { /* key chunk for each iteration */
memset(ks,0,8); /* Clear key schedule */
for (j=0; j<; j++) /* rotate pc1 the right amount */
pcr[j] = pc1m[(l=j+totrot[i])<(j<? : ) ? l: l-];
/* rotate left and right halves independently */
for (j=0; j<; j++){ /* select bits individually */
/* check bit that goes to ks[j] */
if (pcr[pc2[j]-1]){
/* mask it in if it's there */
l= j % 6;
ks[j/6] |= bytebit[l] >> 2;
}
}
/* Now convert to odd/even interleaved form for use in F */
k[2*i] = ((word)ks[0] << )
| ((word)ks[2] << )
| ((word)ks[4] << 8)
| ((word)ks[6]);
k[2*i+1] = ((word)ks[1] << )
| ((word)ks[3] << )
| ((word)ks[5] << 8)
| ((word)ks[7]);
}
if (dir==DECRYPTION) // reverse key schedule order
for (i=0; i<; i+=2)
{
std::swap(k[i], k[-2-i]);
std::swap(k[i+1], k[-1-i]);
}
}
/* End of C code common to both versions */
/* C code only in portable version */
// Richard Outerbridge's initial permutation algorithm
/*
inline void IPERM(word &left, word &right)
{
word work;
work = ((left >> 4) ^ right) & 0x0f0f0f0f;
right ^= work;
left ^= work << 4;
work = ((left >> ) ^ right) & 0xffff;
right ^= work;
left ^= work << ;
work = ((right >> 2) ^ left) & 0x;
left ^= work;
right ^= (work << 2);
work = ((right >> 8) ^ left) & 0xffff;
left ^= work;
right ^= (work << 8);
right = rotl(right, 1);
work = (left ^ right) & 0xaaaaaaaa;
left ^= work;
right ^= work;
left = rotl(left, 1);
}
inline void FPERM(word &left, word &right)
{
word work;
right = rotr(right, 1);
work = (left ^ right) & 0xaaaaaaaa;
left ^= work;
right ^= work;
left = rotr(left, 1);
work = ((left >> 8) ^ right) & 0xffff;
right ^= work;
left ^= work << 8;
work = ((left >> 2) ^ right) & 0x;
right ^= work;
left ^= work << 2;
work = ((right >> ) ^ left) & 0xffff;
left ^= work;
right ^= work << ;
work = ((right >> 4) ^ left) & 0x0f0f0f0f;
left ^= work;
right ^= work << 4;
}
*/
// Wei Dai's modification to Richard Outerbridge's initial permutation
// algorithm, this one is faster if you have access to rotate instructions
// (like in MSVC)
inline void IPERM(word &left, word &right)
{
word work;
right = rotl(right, 4U);
work = (left ^ right) & 0xf0f0f0f0;
left ^= work;
right = rotr(right^work, U);
work = (left ^ right) & 0xffff;
left ^= work;
right = rotr(right^work, U);
work = (left ^ right) & 0x;
left ^= work;
right = rotr(right^work, 6U);
work = (left ^ right) & 0xffff;
left ^= work;
right = rotl(right^work, 9U);
work = (left ^ right) & 0xaaaaaaaa;
left = rotl(left^work, 1U);
right ^= work;
}
inline void FPERM(word &left, word &right)
{
word work;
right = rotr(right, 1U);
work = (left ^ right) & 0xaaaaaaaa;
right ^= work;
left = rotr(left^work, 9U);
work = (left ^ right) & 0xffff;
right ^= work;
left = rotl(left^work, 6U);
work = (left ^ right) & 0x;
right ^= work;
left = rotl(left^work, U);
work = (left ^ right) & 0xffff;
right ^= work;
left = rotl(left^work, U);
work = (left ^ right) & 0xf0f0f0f0;
right ^= work;
left = rotr(left^work, 4U);
}
// Encrypt or decrypt a block of data in ECB mode
void DES::ProcessBlock(const byte *inBlock, byte * outBlock) const
{
word l,r,work;
#ifdef IS_LITTLE_ENDIAN
l = byteReverse(*(word *)inBlock);
r = byteReverse(*(word *)(inBlock+4));
#else
l = *(word *)inBlock;
r = *(word *)(inBlock+4);
#endif
IPERM(l,r);
const word *kptr=k;
for (unsigned i=0; i<8; i++)
{
work = rotr(r, 4U) ^ kptr[4*i+0];
l ^= Spbox[6][(work) & 0x3f]
^ Spbox[4][(work >> 8) & 0x3f]
^ Spbox[2][(work >> ) & 0x3f]
^ Spbox[0][(work >> ) & 0x3f];
work = r ^ kptr[4*i+1];
l ^= Spbox[7][(work) & 0x3f]
^ Spbox[5][(work >> 8) & 0x3f]
^ Spbox[3][(work >> ) & 0x3f]
^ Spbox[1][(work >> ) & 0x3f];
work = rotr(l, 4U) ^ kptr[4*i+2];
r ^= Spbox[6][(work) & 0x3f]
^ Spbox[4][(work >> 8) & 0x3f]
^ Spbox[2][(work >> ) & 0x3f]
^ Spbox[0][(work >> ) & 0x3f];
work = l ^ kptr[4*i+3];
r ^= Spbox[7][(work) & 0x3f]
^ Spbox[5][(work >> 8) & 0x3f]
^ Spbox[3][(work >> ) & 0x3f]
^ Spbox[1][(work >> ) & 0x3f];
}
FPERM(l,r);
#ifdef IS_LITTLE_ENDIAN
*(word *)outBlock = byteReverse(r);
*(word *)(outBlock+4) = byteReverse(l);
#else
*(word *)outBlock = r;
*(word *)(outBlock+4) = l;
#endif
}
void DES_EDE_Encryption::ProcessBlock(byte *inoutBlock) const
{
e.ProcessBlock(inoutBlock);
d.ProcessBlock(inoutBlock);
e.ProcessBlock(inoutBlock);
}
void DES_EDE_Encryption::ProcessBlock(const byte *inBlock, byte *outBlock) const
{
e.ProcessBlock(inBlock, outBlock);
d.ProcessBlock(outBlock);
e.ProcessBlock(outBlock);
}
void DES_EDE_Decryption::ProcessBlock(byte *inoutBlock) const
{
d.ProcessBlock(inoutBlock);
e.ProcessBlock(inoutBlock);
d.ProcessBlock(inoutBlock);
}
void DES_EDE_Decryption::ProcessBlock(const byte *inBlock, byte *outBlock) const
{
d.ProcessBlock(inBlock, outBlock);
e.ProcessBlock(outBlock);
d.ProcessBlock(outBlock);
}
void TripleDES_Encryption::ProcessBlock(byte *inoutBlock) const
{
e1.ProcessBlock(inoutBlock);
d.ProcessBlock(inoutBlock);
e2.ProcessBlock(inoutBlock);
}
void TripleDES_Encryption::ProcessBlock(const byte *inBlock, byte *outBlock) const
{
e1.ProcessBlock(inBlock, outBlock);
d.ProcessBlock(outBlock);
e2.ProcessBlock(outBlock);
}
void TripleDES_Decryption::ProcessBlock(byte *inoutBlock) const
{
d1.ProcessBlock(inoutBlock);
e.ProcessBlock(inoutBlock);
d2.ProcessBlock(inoutBlock);
}
void TripleDES_Decryption::ProcessBlock(const byte *inBlock, byte *outBlock) const
{
d1.ProcessBlock(inBlock, outBlock);
e.ProcessBlock(outBlock);
d2.ProcessBlock(outBlock);
}
NAMESPACE_END
Ubuntu 9.ä¸å®è£ å使ç¨ffmpegç详ç»è¿ç¨ï¼
ffmpegçç¼è¯ãå®è£ å使ç¨(for x,for arm)
/iminway/blog/item/bda2db4ecedb2def1.html
ææ°çffmpegå¯ä»¥éè¿svnä¸è½½ï¼SVNè¾ å©ç软件æï¼
SubVersionï¼ä» /modules/mydownloads/ï¼pile --cc=arm-linux-gcc --arch=arm --enable-gpl --disable-strip --disable-network --disable-ipv6 --disable-vhook --disable-audio-beos --disable-audio-oss --disable-mpegaudio-hp --enable-pthreads --enable-small --disable-parsers --disable-debug
#make
#make install
xä¸çldconfigä¸è½å¨armä¸è¿è¡ï¼armä¸çldconfigå·¥å ·æ¯å¨å»ºç«äº¤åç¼è¯å¨æ¶ï¼ç¼è¯glibcæ¯äº§ççï¼å¯ä»¥æ·è´å°arm-linuxä¸ã
(4) æ¬æ大é¨åå 容æ¥èªç½ç»ï¼å ¶ä¸xvid,xçåºï¼æ亲æå®è£ è¿ï¼ffmpegçé ç½®ç¼è¯for xçç®æé ç½®ï¼for armï¼æ亲æé ç½®ç¼è¯è¿ï¼å¹¶å¨xï¼armä¸å¯ç¨ï¼ç¼è¯é ç½®é½æ¯éç¨éæåºã
ffmpegä½ä¸ºåªä½æ件å¤ç软件ï¼åºæ¬ç¨æ³å¦ä¸ï¼
ffmpeg -i INPUTfile [OPTI
æç« åºå¤ï¼DIYé¨è½(/course/6_system/linux/Linuxjs//.html)
C#用datagriview控件增加删除行数据,并且更新到xml中,求代码(急)如图
完全按你的要求写的,供参考:前提是XML文件已创建好,在窗体里选择该XML文件,再行操作:
窗体:
//选择XML按钮private void btnXML_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
OpenFileDialog xmlFile = new OpenFileDialog();
xmlFile.Filter = "*.xml|";
xmlFile.Title = "请选择xml文件";
if (xmlFile.ShowDialog() == DialogResult.OK)
{
this.txtPath.Text = xmlFile.FileName;//在textBox1控件中显示选择的路径
dt=GetXML(this.txtPath.Text);
}
if(!dt.Equals(null)&&dt.Rows.Count>0)
LoadData(dt, dgvXML);//加载到窗体中
}
//保存按钮
private void button1_Click(object sender, EventArgs e)
{
int n = SaveDgv();
if (n > 0)
{
MessageBox.Show("保存成功!");
DataTable dt = new DataTable();
dt = GetXML(this.txtPath.Text);
LoadData(dt, dgvXML);//刷新加载
}
else
{
MessageBox.Show("保存失败!");
DataTable dt = new DataTable();
dt = GetXML(this.txtPath.Text);
LoadData(dt, dgvXML);//刷新加载
}
}
//删除按钮
private void btnDelete_Click(object sender, EventArgs e)
{
int n = 0;//判断是否删除
for (int i = 0; i < dgvXML.SelectedRows.Count; i++)
{
string id = dgvXML.SelectedRows[i].Cells[0].Value.ToString();
XmlDocument doc = new XmlDocument();
doc.Load(this.txtPath.Text);
XmlNodeList nod = doc.DocumentElement.ChildNodes;
XmlNode root = doc.DocumentElement;
foreach (XmlNode peo in nod)
{
if (XmlNode.Equals(peo.SelectSingleNode("ID").InnerText, id))
{
root.RemoveChild(peo);//从根节点删除该ID的PERSON
doc.Save(this.txtPath.Text);
n++;
}
}
}
if (n > 0)
{
DataTable dt = new DataTable();
dt = GetXML(this.txtPath.Text);
LoadData(dt, dgvXML);//刷新加载
MessageBox.Show("删除成功!");
}
else
{
MessageBox.Show("删除失败!");
}
}
// DataGridView中加载数据
private void LoadData(DataTable dt, DataGridView dgv)
{
dgv.Rows.Clear();//datagridview清空数据
if (dt != null && dt.Rows.Count > 0)
{
dgv.Rows.Add(dt.Rows.Count);
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int k = 0; k < dt.Columns.Count; k++)
{
dgv.Rows[i].Cells[k].Value = dt.Rows[i][k].ToString();//赋值
}
}
}
}
//datagridview添加行号
private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
{
e.Row.HeaderCell.Value = (e.Row.Index + 1).ToString();
}
//解析XML生成DATATABLE
private List<string> IDList = new List<string>();//用来记录已有的ID,以在修改时判断是否新增
private DataTable GetXML(string path)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("NAME");
dt.Columns.Add("SEX");
dt.Columns.Add("PHONE");
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNodeList nod = doc.DocumentElement.ChildNodes;
foreach (XmlNode peo in nod)
{
XmlNodeList pe = peo.ChildNodes;
DataRow row = dt.NewRow();
foreach (XmlNode p in pe)
{
if (p.Name == "ID")
{
row["ID"] = p.InnerText.ToString();
IDList.Add(p.InnerText.ToString());//添加ID记录,以在修改时判断是否新增
}
if (p.Name == "NAME")
{
row["NAME"] = p.InnerText.ToString();
}
if (p.Name == "SEX")
{
row["SEX"] = p.InnerText.ToString();
}
if (p.Name == "PHONE")
{
row["PHONE"] = p.InnerText.ToString();
}
}
dt.Rows.Add(row);
}
return dt;
}
//修改dgv数据后保存到XML文件
private int SaveDgv()
{
int n = 0;
for (int i = 0; i < dgvXML.Rows.Count-1; i++)
{
XmlDocument doc = new XmlDocument();
doc.Load(this.txtPath.Text);
XmlNodeList nod = doc.DocumentElement.ChildNodes;
string id = dgvXML.Rows[i].Cells[0].Value.ToString();
if (IDList.Contains(id)) //如果是已有的ID,则修改
{
foreach (XmlNode peo in nod)
{
if (XmlNode.Equals(peo.SelectSingleNode("ID").InnerText, id))
{
peo.SelectSingleNode("NAME").InnerText = dgvXML.Rows[i].Cells[1].Value.ToString();//赋值姓名
peo.SelectSingleNode("SEX").InnerText = dgvXML.Rows[i].Cells[2].Value.ToString();//赋值性别
peo.SelectSingleNode("PHONE").InnerText = dgvXML.Rows[i].Cells[3].Value.ToString();//赋值电话
doc.Save(this.txtPath.Text);
n++;
break;//当找到该ID记录并修改后,跳出循环
}
}
}
else //否则为新增
{
XmlNode root = doc.DocumentElement;
XmlElement PERSON = doc.CreateElement("PERSON");
XmlElement ID = doc.CreateElement("ID");
ID.InnerText = id;//赋值id
XmlElement NAME = doc.CreateElement("NAME");
NAME.InnerText = dgvXML.Rows[i].Cells[1].Value.ToString();//赋值姓名
XmlElement SEX = doc.CreateElement("SEX");
SEX.InnerText = dgvXML.Rows[i].Cells[2].Value.ToString();//赋值性别
XmlElement PHONE = doc.CreateElement("PHONE");
PHONE.InnerText = dgvXML.Rows[i].Cells[3].Value.ToString();//赋值电话
PERSON.AppendChild(ID);
PERSON.AppendChild(NAME);
PERSON.AppendChild(SEX);
PERSON.AppendChild(PHONE);
root.AppendChild(PERSON);
doc.Save(this.txtPath.Text);//保存
n++;
}
}
return n;
}
XML文件:
<?xml version="1.0" standalone="yes"?>
<List>
<PERSON>
<ID></ID>
<NAME>测一</NAME>
<SEX>男男</SEX>
<PHONE></PHONE>
</PERSON>
<PERSON>
<ID></ID>
<NAME>再二</NAME>
<SEX>女女</SEX>
<PHONE></PHONE>
</PERSON>
<PERSON>
<ID></ID>
<NAME>张三</NAME>
<SEX>男男</SEX>
<PHONE></PHONE>
</PERSON>
<PERSON>
<ID></ID>
<NAME>王五</NAME>
<SEX>女女</SEX>
<PHONE></PHONE>
</PERSON>
</List>