XML 读
StringBuilder output = new
StringBuilder();
String xmlString =
@"<bookstore>
<book genre='novel' ISBN='10-861003-324'>
<title>The Handmaid's Tale</title>
<price>19.95</price>
</book>
<book genre='novel' ISBN='1-861001-57-5'>
<title>Pride And Prejudice</title>
<price>24.95</price>
</book>
</bookstore>";
// Load the file and ignore all white space.
XmlReaderSettings settings = new
XmlReaderSettings();
settings.IgnoreWhitespace = true
;
using
(XmlReader reader = XmlReader.Create(new
StringReader(xmlString), settings))
{
// Move the reader to the second book node.
reader.MoveToContent();
reader.ReadToDescendant("book"
);
reader.Skip();
//Skip the first book.
// Parse the file starting with the second book node.
do
{
switch
(reader.NodeType)
{
case
XmlNodeType.Element:
output.AppendLine("<"
+ reader.Name);
while
(reader.MoveToNextAttribute())
{
output.AppendLine(" "
+ reader.Name + "="
+ reader.Value);
}
output.AppendLine(">"
);
break
;
case
XmlNodeType.Text:
output.AppendLine(reader.Value);
break
;
case
XmlNodeType.EndElement:
output.AppendLine("</"
+ reader.Name + ">"
);
break
;
}
}
while
(reader.Read());
}
OutputTextBlock.Text = output.ToString();
XML是目前最常用的通用数据传输与处理接口类型。本文介绍如何用C#.NET读写XML文档资料。
<?xml version="1.0" encoding="utf-8"?>
相关文档:
XML Schema attributeGroup 元素
定义和用法
attributeGroup 元素用于对属性声明进行组合,这样这些声明就能够以组合的形式合并到复杂类型中。
元素信息
出现次数
无限制
父元素
attributeGroup、complexType、schema、restriction (simpleContent)、extension (simpleContent)、rest ......
XML解析器的作用:为应用程序从XML文件中解析出所需要的数据。
下面通过一个例子,来了解,如何用XML解析器,来解析一个XML文件中的数据。
1、Types.xml(显示吉他的类别)
<?xml version="1.0" encoding="UTF-8"?>
<types>
<name>电吉他
<music>玩摇滚</music>
</name>
......
1)Xml文档示例(xmlsample.xml):
Code
<?xml version="1.0" encoding="iso-8859-1" ?>
<music>
<song title="Oh,girl">
<artist>The Chi-lites</artist>
<genre>Soul</genre>
&nb ......
/// <summary>
/// 支持XML序列化的泛型 Dictionary
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
[XmlRoot("SerializableDictionary")]
public class SerializableDictionary<TKey, TValue& ......
获取Spring框架管理的类实例的方法有多种,如下:
方法一:在初始化时保存ApplicationContext对象
代码:
ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");
ac.getBean("beanId");
说明:
这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化 ......