柚子Nan--回归原点

Everything can be as easy as you like or as complex as you need.
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

关于如何创建属性和元素,还有如何引用实体

Posted on 2004-04-22 14:02  柚子Nan  阅读(883)  评论(0)    收藏  举报

XMLTextWrite类的一点简单的代码,

 

string fileName = @"d:\open.xml";

XmlTextWriter xmlw = new XmlTextWriter(fileName,System.Text.Encoding.Default);

xmlw.Formatting = Formatting.Indented;

 

xmlw.WriteStartDocument(true);

xmlw.WriteDocType("MyDef",null,null,"<!ENTITY I 'Italy'><!ENTITY I-Capital  'Rome'>");

 

                            xmlw.WriteStartElement("Cities");

                            xmlw.WriteStartElement("City");

                            xmlw.WriteAttributeString("Zip","201818");

 

                            xmlw.WriteStartAttribute("State",null);

                            xmlw.WriteEntityRef("I");

                            xmlw.WriteEndAttribute();

 

                            xmlw.WriteEntityRef("I-Capital");

 

                            xmlw.WriteEndElement();

                            xmlw.WriteEndElement();

 

                            xmlw.WriteEndDocument();

 

xmlw.Close();

 

输出的结果如下:

<?xml version="1.0" encoding="gb2312" standalone="yes" ?>

  <!DOCTYPE MyDef (View Source for full doctype...)>

- <Cities>

      <City Zip="201818" State="Italy">Rome</City>

  </Cities>


很明显,属性和元素的区别。
State="Italy"是属性定义,Rome属于元素定义也就是节点