��.NET Framework�����ɴ���XML����(��)
[����]XmlTextWriter�� ���ڱ����еķ�������XML�ĵ���Ȼ�������ѡ����������������߶���ͨ���ڻ���������һЩ�ַ��������Ӻ��Ժ��ٰѻ������ַ����������ļ��ķ�ʽ������XML�ĵ������������ַ�ʽ����XML�ĵ��ķ���ֻ������...
XmlTextWriter��
���ڱ����еķ�������XML�ĵ���Ȼ�������ѡ����������������߶���ͨ���ڻ���������һЩ�ַ��������Ӻ��Ժ��ٰѻ������ַ����������ļ��ķ�ʽ������XML�ĵ������������ַ�ʽ����XML�ĵ��ķ���ֻ�����㱣֤�ַ����в������κ�ϸС�Ĵ�����ʱ������Ч��.NET Frameworkͨ����XMLwriter�ṩ�˸��õĴ���XML�ĵ��ķ�����
XML Writer����ֻǰ��forward-only���ķ�ʽ����XML���ݵ��������ļ��С�����Ҫ���ǣ�XML Writer������ʱ�ͱ�֤���е�XML���ݶ�����W3C XML 1.0�Ƽ��淶�����������õ�������д�ձ�ǩ����ΪXML Writer������д��XmlWriter������ XML writer�ij������ࡣ.NET Frameworkֻ�ṩΨһ��һ��writer ��----XmlTextWriter�ࡣ
������������XML writers�;ɵ�writers�IJ�ͬ�㣬�����Ĵ��뱣����һ��string�͵����飺
StringBuilder sb = new StringBuilder("");
sb.Append("<array>");
foreach(string s in theArray) {
sb.Append("<element value=""");
sb.Append(s);
sb.Append("""/>");
}
sb.Append("</array>");
����ͨ��ѭ��ȡ�������е�Ԫ�أ�д�ñ�ǩ�ı����������ۼӵ�һ��string�С����뱣֤�����������Ǹ�ʽ���õIJ���ע�������е���������֧�������ռ䡣���������ĵ��ṹ�Ƚϼ���ʱ�����ַ������ܲ����д�����Ȼ��������Ҫ֧�ִ���ָ������ռ䣬��������ʽ���Լ�ʵ����ʱ�����������ͳ�ָ���������������Ŀ�����Ҳ��֮������
XML writerд�������ܶ�Ӧÿ�����ܵ�XML�ڵ����ͣ���ʹ����xml�ĵ��Ĺ��̸������������ٵ������ڷ����ı������ԡ�ͼ����ʾ����ô����XmlTextWriter���ķ���������һ��string���ݡ������ܼ��࣬��XML writer�Ĵ������������ṹ���á�
Figure 6 Serializing a String Array
void CreateXmlFileUsingWriters(String[] theArray, string filename)
{
// Open the XML writer (��Ĭ�ϵ��ַ���)
XmlTextWriter xmlw = new XmlTextWriter(filename, null);
xmlw.Formatting = Formatting.Indented;
xmlw.WriteStartDocument();
xmlw.WriteStartElement("array");
foreach(string s in theArray)
{
xmlw.WriteStartElement("element");
xmlw.WriteAttributeString("value", s);
xmlw.WriteEndElement();
}
xmlw.WriteEndDocument();
// Close the writer
xmlw.Close();
}
Ȼ��XML writer������ħ��ʦ----�������������Ĵ�����XML writer��������Ԫ�������������Ƿ���Ч��Ҳ����֤���õ��κε�Unicode�ַ����ʺϵ�ǰ�ܹ��ı��뼯������������Ϊ�˱�������������Ҫ�ž���XML�ַ�������writerû���ṩ���ַ�����
���⣬������һ�����Խڵ�ʱ��Writer�����������Խڵ��������Ƿ����Ѵ��ڵ�Ԫ�ؽڵ���������ͬ��������XmlWriter���һ������֤��Writer�࣬Ҳ����֤�����Ƿ�����schema����DTD����.NET Framework�д���֤��writer��Ŀǰ��˵��û���ṩ����������д�ġ�Applied XML Programming for Microsoft .NET (Microsoft Press®, 2002)�����У����Լ�д��һ������֤��Writer�����������Ե���������ַȥ����Դ�룺http://www.microsoft.com/MSPress/books/6235.asp.
ͼ���г���XML writer��һЩ״ֵ̬��state������Щֵ��Դ��WriteStateö���ࡣ���㴴��һ��Writer�����ij�ʼ״̬ΪStart����ʾ�㽫Ҫ���øö�����ʵ����writerû�п�ʼ����һ��״̬��Prolog����״̬�ǵ�������WriteStartDocument������ʼ������ʱ�����õġ�Ȼ����״̬��ת����ȡ��������д���ĵ����ĵ��������ˡ�Prolog״̬һֱ��������������һ����Ԫ�ؽڵ�ʱ������ע��Ԫ�أ�����ָ��ĵ����͡�����һ���ڵ�Ҳ���Ǹ��ڵ�д������״̬�ͱ�ΪElement����������WriterStartAtribute����ʱ״̬ת��ΪAttribute�������ǵ�������WriteAtributeString����д����ʱת��Ϊ��״̬�����������Ļ���״̬Ӧ����Element������дһ���ձ�ǩ(>)ʱ��״̬��ת����Content������д���ĵ�����WriteEndDocument������״̬�ͻ᷵��ΪStart,ֱ���㿪ʼд��һ���ĵ����߰�Writer�ص���
Figure 7 States for XML Writer
State
Description
Attribute
The writer enters this state when an attribute is being written
Closed
The Close method has been called and the writer is no longer available for writing operations
Content
The writer enters this state when the content of a node is being written
Element
The writer enters this state when an element start tag is being written
Prolog
The writer is writing the prolog of a well-formed XML 1.0 document
Start
The writer is in an initial state, awaiting for a write call to be issued
Writer �������ı������ڲ���һ���������ڡ�һ�������£��������ᱻˢ�»��߱���������Writer���ر�ǰXML�ı�Ӧ��Ҫд�������κ�ʱ�㶼����ͨ������Flush�������ջ��������ѵ�ǰ������д�����У�ͨ��BaseStream���Ա�¶������Ȼ���ͷŲ���ռ�õ��ڴ棬Writer�Ա���Ϊ����״̬��open state�������Լ���������ע�⣬��Ȼд�˲��ֵ��ĵ����ݣ�������Writerû�йر�ǰ�����ij����Dz��ܴ������ĵ��ġ�
���������ַ�����д���Խڵ㡣��һ�ַ�������WriteStartAtribute����ȥ����һ���µ����Խڵ㣬����Writer��״̬��������WriteString������������ֵ��д��������WriteEndElement���������ýڵ㡣���⣬��Ҳ������WriteAttributeString����ȥ�����µ����Խڵ㣬��writerr��״̬ΪElementʱ��WriterAttributeString��ʼ����������������һ�����ԡ�ͬ���ģ�WriteStartElement����д�ڵ��Ŀ�ʼ��ǩ��<����Ȼ�����������������ýڵ������Ժ��ı����ݡ�Ԫ�ؽڵ��ıձ�ǩ������/ >����������д�ձ�ǩ������WriteFullEndElement������д��
Ӧ�ñ����д�������ı��а������еı����ַ�������С�ںţ�<������WriteRaw����д�������ַ������ᱻ���������ǿ�����������xml�ĵ�д���������ַ��������������д��룬��һ���������ǡ�<�����ڶ���������<��:
writer.WriteString("<");
writer.WriteRaw("<");
���
��Ȥ���ǣ�reader(�Ķ���)��writer���ṩ�˻���Base64 ��BinHex�����Ķ�д�������ķ�����WriteBase64 �� WriteBinHex�����Ĺ�����������д�����Ĺ��ܴ�����ϸ�IJ��������Ƕ��ǻ������ģ������������Ĺ�����һ��byte����������һ��string�������Ĵ������Ȱ�һ��stringת����һ��byte���飬Ȼ��������д��һ��Base64 ��������Encoding����GetBytes��̬��������ת����������
writer.WriteBase64(
Encoding.Unicode.GetBytes(buf),
0, buf.Length*2);
ͼ���д�����ʾ�˰�һ��string����ת��ΪBase64 ������XML����ͼ���������Ľ�����
Figure 8 Persisting a String Array as Base64
using System;
using System.Text;
using System.IO;
using System.Xml;
class MyBase64Array
{
public static void Main(String[] args)
{
string outputFileName = "test64.xml";
if (args.Length > 0)
outputFileName = args[0]; // file name
// ������ת����XML
String[] theArray = {"Rome", "New York", "Sydney", "Stockholm",
"Paris"};
CreateOutput(theArray, outputFileName);
return;
}
private static void CreateOutput(string[] theArray, string filename)
{
// ����XML writer
XmlTextWriter xmlw = new XmlTextWriter(filename, null);
//ʹ��Ԫ�ظ��� Indentation �� IndentChar ������������ѡ��ֻ��Ԫ�����ݽ�������
xmlw.Formatting = Formatting.Indented;
//��д�汾Ϊ��1.0���� XML ����
xmlw.WriteStartDocument();
//д������ָ���ı���ע�� <!--...-->��
xmlw.WriteComment("Array to Base64 XML");
//��ʼд��array�ڵ�
xmlw.WriteStartElement("array");
//д������ָ����ǰ���������ơ������ռ� URI ��ֵ������
xmlw.WriteAttributeString("xmlns", "x", null, "dinoe:msdn-mag");
// ѭ����д��array���ӽڵ�
foreach(string s in theArray)
{
//д��ָ���Ŀ�ʼ���Dz������������������ռ���ǰ��������
xmlw.WriteStartElement("x", "element", null);
//��Sת����byte[]����, ����byte[]��������Ϊ Base64 ��д�������ı���Ҫд�����ֽ���Ϊs�ܳ��ȵ�2��,һ��stringռ���ֽ�����2�ֽڡ�
xmlw.WriteBase64(Encoding.Unicode.GetBytes(s), 0, s.Length*2);
//�ر��ӽڵ�
xmlw.WriteEndElement();
}
//�رո��ڵ㣬ֻ������
xmlw.WriteEndDocument();
// �ر�writer
xmlw.Close();
// �����������
XmlTextReader reader = new XmlTextReader(filename);
while(reader.Read())
{
//��ȡ�ڵ���Ϊelement�Ľڵ�
if (reader.LocalName == "element")
{
byte[] bytes = new byte[1000];
int n = reader.ReadBase64(bytes, 0, 1000);
string buf = Encoding.Unicode.GetString(bytes);
Console.WriteLine(buf.Substring(0,n));
}
}
reader.Close();
}
}
Figure 9 String Array in Internet Explorer
Reader����ר�ŵĽ���Base64��BinHex�������ķ����������Ĵ���Ƭ����ʾ����ô����XmlTextReader����ReadBase64����������Base64��BinHex���뼯�������ĵ���
XmlTextReader reader = new XmlTextReader(filename);
while(reader.Read()) {
if (reader.LocalName == "element") {
byte[] bytes = new byte[1000];
int n = reader.ReadBase64(bytes, 0, 1000);
string buf = Encoding.Unicode.GetString(bytes);
Console.WriteLine(buf.Substring(0,n));
}
}
reader.Close();
��byte��ת����string����ͨ��Encoding����GetString����ʵ�ֵġ�������ֻ�����˻���Base64���뼯�Ĵ��룬���ǿ��Լ���BinHex�滻�������Ϳ���ʵ�ֶ�����BinHex�����Ľڵ����ݣ���ReadBinHex����������������Ҳ�������ڶ��κ���byte������ʽ��ʾ�Ķ��������ݣ�������image���͵����ݡ�
���ڱ����еķ�������XML�ĵ���Ȼ�������ѡ����������������߶���ͨ���ڻ���������һЩ�ַ��������Ӻ��Ժ��ٰѻ������ַ����������ļ��ķ�ʽ������XML�ĵ������������ַ�ʽ����XML�ĵ��ķ���ֻ�����㱣֤�ַ����в������κ�ϸС�Ĵ�����ʱ������Ч��.NET Frameworkͨ����XMLwriter�ṩ�˸��õĴ���XML�ĵ��ķ�����
XML Writer����ֻǰ��forward-only���ķ�ʽ����XML���ݵ��������ļ��С�����Ҫ���ǣ�XML Writer������ʱ�ͱ�֤���е�XML���ݶ�����W3C XML 1.0�Ƽ��淶�����������õ�������д�ձ�ǩ����ΪXML Writer������д��XmlWriter������ XML writer�ij������ࡣ.NET Frameworkֻ�ṩΨһ��һ��writer ��----XmlTextWriter�ࡣ
������������XML writers�;ɵ�writers�IJ�ͬ�㣬�����Ĵ��뱣����һ��string�͵����飺
StringBuilder sb = new StringBuilder("");
sb.Append("<array>");
foreach(string s in theArray) {
sb.Append("<element value=""");
sb.Append(s);
sb.Append("""/>");
}
sb.Append("</array>");
����ͨ��ѭ��ȡ�������е�Ԫ�أ�д�ñ�ǩ�ı����������ۼӵ�һ��string�С����뱣֤�����������Ǹ�ʽ���õIJ���ע�������е���������֧�������ռ䡣���������ĵ��ṹ�Ƚϼ���ʱ�����ַ������ܲ����д�����Ȼ��������Ҫ֧�ִ���ָ������ռ䣬��������ʽ���Լ�ʵ����ʱ�����������ͳ�ָ���������������Ŀ�����Ҳ��֮������
XML writerд�������ܶ�Ӧÿ�����ܵ�XML�ڵ����ͣ���ʹ����xml�ĵ��Ĺ��̸������������ٵ������ڷ����ı������ԡ�ͼ����ʾ����ô����XmlTextWriter���ķ���������һ��string���ݡ������ܼ��࣬��XML writer�Ĵ������������ṹ���á�
Figure 6 Serializing a String Array
void CreateXmlFileUsingWriters(String[] theArray, string filename)
{
// Open the XML writer (��Ĭ�ϵ��ַ���)
XmlTextWriter xmlw = new XmlTextWriter(filename, null);
xmlw.Formatting = Formatting.Indented;
xmlw.WriteStartDocument();
xmlw.WriteStartElement("array");
foreach(string s in theArray)
{
xmlw.WriteStartElement("element");
xmlw.WriteAttributeString("value", s);
xmlw.WriteEndElement();
}
xmlw.WriteEndDocument();
// Close the writer
xmlw.Close();
}
Ȼ��XML writer������ħ��ʦ----�������������Ĵ�����XML writer��������Ԫ�������������Ƿ���Ч��Ҳ����֤���õ��κε�Unicode�ַ����ʺϵ�ǰ�ܹ��ı��뼯������������Ϊ�˱�������������Ҫ�ž���XML�ַ�������writerû���ṩ���ַ�����
���⣬������һ�����Խڵ�ʱ��Writer�����������Խڵ��������Ƿ����Ѵ��ڵ�Ԫ�ؽڵ���������ͬ��������XmlWriter���һ������֤��Writer�࣬Ҳ����֤�����Ƿ�����schema����DTD����.NET Framework�д���֤��writer��Ŀǰ��˵��û���ṩ����������д�ġ�Applied XML Programming for Microsoft .NET (Microsoft Press®, 2002)�����У����Լ�д��һ������֤��Writer�����������Ե���������ַȥ����Դ�룺http://www.microsoft.com/MSPress/books/6235.asp.
ͼ���г���XML writer��һЩ״ֵ̬��state������Щֵ��Դ��WriteStateö���ࡣ���㴴��һ��Writer�����ij�ʼ״̬ΪStart����ʾ�㽫Ҫ���øö�����ʵ����writerû�п�ʼ����һ��״̬��Prolog����״̬�ǵ�������WriteStartDocument������ʼ������ʱ�����õġ�Ȼ����״̬��ת����ȡ��������д���ĵ����ĵ��������ˡ�Prolog״̬һֱ��������������һ����Ԫ�ؽڵ�ʱ������ע��Ԫ�أ�����ָ��ĵ����͡�����һ���ڵ�Ҳ���Ǹ��ڵ�д������״̬�ͱ�ΪElement����������WriterStartAtribute����ʱ״̬ת��ΪAttribute�������ǵ�������WriteAtributeString����д����ʱת��Ϊ��״̬�����������Ļ���״̬Ӧ����Element������дһ���ձ�ǩ(>)ʱ��״̬��ת����Content������д���ĵ�����WriteEndDocument������״̬�ͻ᷵��ΪStart,ֱ���㿪ʼд��һ���ĵ����߰�Writer�ص���
Figure 7 States for XML Writer
State
Description
Attribute
The writer enters this state when an attribute is being written
Closed
The Close method has been called and the writer is no longer available for writing operations
Content
The writer enters this state when the content of a node is being written
Element
The writer enters this state when an element start tag is being written
Prolog
The writer is writing the prolog of a well-formed XML 1.0 document
Start
The writer is in an initial state, awaiting for a write call to be issued
Writer �������ı������ڲ���һ���������ڡ�һ�������£��������ᱻˢ�»��߱���������Writer���ر�ǰXML�ı�Ӧ��Ҫд�������κ�ʱ�㶼����ͨ������Flush�������ջ��������ѵ�ǰ������д�����У�ͨ��BaseStream���Ա�¶������Ȼ���ͷŲ���ռ�õ��ڴ棬Writer�Ա���Ϊ����״̬��open state�������Լ���������ע�⣬��Ȼд�˲��ֵ��ĵ����ݣ�������Writerû�йر�ǰ�����ij����Dz��ܴ������ĵ��ġ�
���������ַ�����д���Խڵ㡣��һ�ַ�������WriteStartAtribute����ȥ����һ���µ����Խڵ㣬����Writer��״̬��������WriteString������������ֵ��д��������WriteEndElement���������ýڵ㡣���⣬��Ҳ������WriteAttributeString����ȥ�����µ����Խڵ㣬��writerr��״̬ΪElementʱ��WriterAttributeString��ʼ����������������һ�����ԡ�ͬ���ģ�WriteStartElement����д�ڵ��Ŀ�ʼ��ǩ��<����Ȼ�����������������ýڵ������Ժ��ı����ݡ�Ԫ�ؽڵ��ıձ�ǩ������/ >����������д�ձ�ǩ������WriteFullEndElement������д��
Ӧ�ñ����д�������ı��а������еı����ַ�������С�ںţ�<������WriteRaw����д�������ַ������ᱻ���������ǿ�����������xml�ĵ�д���������ַ��������������д��룬��һ���������ǡ�<�����ڶ���������<��:
writer.WriteString("<");
writer.WriteRaw("<");
���
��Ȥ���ǣ�reader(�Ķ���)��writer���ṩ�˻���Base64 ��BinHex�����Ķ�д�������ķ�����WriteBase64 �� WriteBinHex�����Ĺ�����������д�����Ĺ��ܴ�����ϸ�IJ��������Ƕ��ǻ������ģ������������Ĺ�����һ��byte����������һ��string�������Ĵ������Ȱ�һ��stringת����һ��byte���飬Ȼ��������д��һ��Base64 ��������Encoding����GetBytes��̬��������ת����������
writer.WriteBase64(
Encoding.Unicode.GetBytes(buf),
0, buf.Length*2);
ͼ���д�����ʾ�˰�һ��string����ת��ΪBase64 ������XML����ͼ���������Ľ�����
Figure 8 Persisting a String Array as Base64
using System;
using System.Text;
using System.IO;
using System.Xml;
class MyBase64Array
{
public static void Main(String[] args)
{
string outputFileName = "test64.xml";
if (args.Length > 0)
outputFileName = args[0]; // file name
// ������ת����XML
String[] theArray = {"Rome", "New York", "Sydney", "Stockholm",
"Paris"};
CreateOutput(theArray, outputFileName);
return;
}
private static void CreateOutput(string[] theArray, string filename)
{
// ����XML writer
XmlTextWriter xmlw = new XmlTextWriter(filename, null);
//ʹ��Ԫ�ظ��� Indentation �� IndentChar ������������ѡ��ֻ��Ԫ�����ݽ�������
xmlw.Formatting = Formatting.Indented;
//��д�汾Ϊ��1.0���� XML ����
xmlw.WriteStartDocument();
//д������ָ���ı���ע�� <!--...-->��
xmlw.WriteComment("Array to Base64 XML");
//��ʼд��array�ڵ�
xmlw.WriteStartElement("array");
//д������ָ����ǰ���������ơ������ռ� URI ��ֵ������
xmlw.WriteAttributeString("xmlns", "x", null, "dinoe:msdn-mag");
// ѭ����д��array���ӽڵ�
foreach(string s in theArray)
{
//д��ָ���Ŀ�ʼ���Dz������������������ռ���ǰ��������
xmlw.WriteStartElement("x", "element", null);
//��Sת����byte[]����, ����byte[]��������Ϊ Base64 ��д�������ı���Ҫд�����ֽ���Ϊs�ܳ��ȵ�2��,һ��stringռ���ֽ�����2�ֽڡ�
xmlw.WriteBase64(Encoding.Unicode.GetBytes(s), 0, s.Length*2);
//�ر��ӽڵ�
xmlw.WriteEndElement();
}
//�رո��ڵ㣬ֻ������
xmlw.WriteEndDocument();
// �ر�writer
xmlw.Close();
// �����������
XmlTextReader reader = new XmlTextReader(filename);
while(reader.Read())
{
//��ȡ�ڵ���Ϊelement�Ľڵ�
if (reader.LocalName == "element")
{
byte[] bytes = new byte[1000];
int n = reader.ReadBase64(bytes, 0, 1000);
string buf = Encoding.Unicode.GetString(bytes);
Console.WriteLine(buf.Substring(0,n));
}
}
reader.Close();
}
}
Figure 9 String Array in Internet Explorer
Reader����ר�ŵĽ���Base64��BinHex�������ķ����������Ĵ���Ƭ����ʾ����ô����XmlTextReader����ReadBase64����������Base64��BinHex���뼯�������ĵ���
XmlTextReader reader = new XmlTextReader(filename);
while(reader.Read()) {
if (reader.LocalName == "element") {
byte[] bytes = new byte[1000];
int n = reader.ReadBase64(bytes, 0, 1000);
string buf = Encoding.Unicode.GetString(bytes);
Console.WriteLine(buf.Substring(0,n));
}
}
reader.Close();
��byte��ת����string����ͨ��Encoding����GetString����ʵ�ֵġ�������ֻ�����˻���Base64���뼯�Ĵ��룬���ǿ��Լ���BinHex�滻�������Ϳ���ʵ�ֶ�����BinHex�����Ľڵ����ݣ���ReadBinHex����������������Ҳ�������ڶ��κ���byte������ʽ��ʾ�Ķ��������ݣ�������image���͵����ݡ�
- ת����ע����Դ��ITѧϰ�� ��ַ��http://www.t086.com/ �����������Ƽ�������
- �ر������� ��վ�������ر�������ֹת�ص�ר�������������¿�������ת�أ���������ע��������ԭʼ���ߡ����°�Ȩ������ԭʼ�������С����ڱ���վת�����µĸ��˺���վ�����DZ�ʾ������л�⡣������վת�ص������а�Ȩ��������ϵ���ǣ����ǻᾡ�����Ը�����
- �������е���ɫ
- �������ж�IP��ַ��A��B���C��
- ������ϵ��Ц�� (ʷ����ȫ��
- ��Google���������ŵ���ͨv4.0
- ���ٶ�ͳ��URL��hm������utm�����Ķ�Ӧ��ϵ
- ��ƻ������MACbook Air���ݼ���ȫ
- ���������ԡ���װ Windows XP ����ϵͳ��ͼ��ȫ����
- ��Linux��ʹ��host��dig��nslookup��ѯDNS
- �����Լ���VPS�Google��������
- ��С�ڴ�VPS֮Nginx+PHP-fpm�߸����Ż���ѹ�����Է���
- ��������������ѡʳƷ����2020�ļ��ȵ�Ͷ�ʲ��Ժ���������
- ���ٶȽ�����ȷʶ���ٶ�֩��
- ��ͼƬ�Ż��Ĺ����Ƽ�
- �������˴�ҵ��վ����ָ��(�ڶ���)
- ��Linux�ļ�ϵͳֻ��Read-only file system�Ŀ��ٽ�������
- ��win2000server�����ܹ��������������أ�ֱ�ӽ��룿
- ��linux�´�����ɾ������Ӳ����
- ��linux ��������ǰ��̨�л�
- ��������վ��YISOU���Σ����ν����ͷ��Ļظ�
- ����ַ��ȫ����ȫ��4000������ַPR��alexa��Ϣ