web前端

位置:IT落伍者 >> web前端 >> 浏览文章

XmlReader 读取器读取内存流


发布日期:2022年03月18日
 
XmlReader 读取器读取内存流
MemoryStream对象提供了无需进行IO就可以创建Stream的方法XmlTextWriter和XmlReader提供快速书写和读取XML内容的方法结合MemoryStream就可以直接在内存中构造XmlTextWriter并用XmlReader进行读取

使用MemoryStream和XmlTextWriter进行书写XML需要注意两点XmlTextWriterFlush操作和重设MemoryStreamPosition =

C#

<%@ Page Language=C#%>

<%@ Import Namespace=SystemXml %>

<%@ Import Namespace=SystemIO %>

<!DOCTYPE html PUBLIC //WC//DTD XHTML Transitional//EN transitionaldtd>

<script runat=server>

protected void Button_Click(object sender EventArgs e)

{

ResponseClear();

ResponseContentType = text/xml;

MemoryStream msXml = new MemoryStream();

XmlTextWriter xmlWriter = new XmlTextWriter(msXml EncodingUTF);

xmlWriterWriteStartElement(rss);

xmlWriterWriteAttributeString(version );

xmlWriterWriteStartElement(channel);

xmlWriterWriteElementString(title 【孟宪会之精彩世界】);

xmlWriterWriteElementString(link /Rssaspx);

xmlWriterWriteElementString(description NET开发技术);

xmlWriterWriteElementString(language zhCN);

xmlWriterWriteElementString(copyright Copyright 【孟宪会之精彩世界】);

xmlWriterWriteElementString(managingEditor amxh[AT]);

xmlWriterWriteStartElement(image);

xmlWriterWriteElementString(title 【孟宪会之精彩世界】);

xmlWriterWriteElementString(width );

xmlWriterWriteElementString(height );

xmlWriterWriteElementString(link /);

xmlWriterWriteElementString(url _//gif);

xmlWriterWriteEndElement();

//循环读出数据库内容列表忽略

//while (objReaderRead())

//{

// xmlWriterWriteStartElement(item);

// xmlWriterWriteElementString(title objReaderGetString());

// xmlWriterWriteElementString(description objReaderGetString());

// xmlWriterWriteElementString(link /article/abceecadc/readaspx);

// xmlWriterWriteElementString(pubDate objReaderGetDateTime()ToString(G));

// xmlWriterWriteEndElement();

//}

//objReaderClose();

xmlWriterWriteStartElement(item);

xmlWriterWriteElementString(title ASPNET 中直接将Access数据库导入到Excel文件中);

xmlWriterWriteElementString(description ASPNET 中直接将Access数据库导入到Excel文件中);

xmlWriterWriteElementString(link /article/abceecadc/readaspx);

xmlWriterWriteElementString(pubDate Sat Dec :: GMT);

xmlWriterWriteEndElement();

xmlWriterWriteEndElement();

xmlWriterWriteEndElement();

xmlWriterFlush(); // 确保书写器更新到Stream中

msXmlPosition = ; // 重置流的位置以便我们可以从头读取

XmlReader xmlReader = XmlReaderCreate(msXml);

while (xmlReaderRead())

{

if (xmlReaderName == rss)

{

ResponseWrite(xmlReaderReadOuterXml());

}

}

ResponseEnd();

msXmlClose();

xmlWriterClose();

xmlReaderClose();

}

</script>

<html xmlns=>

<head runat=server>

<title>XmlReader 读取器读取内存流 MemoryStream 的注意事项</title>

</head>

<body>

<form id=form runat=server>

<asp:Button ID=Button runat=server OnClick=Button_Click Text=读取数据 />

</form>

</body>

</html>

VBNET

Protected Sub Button_Click(ByVal sender As Object ByVal e As EventArgs)

ResponseClear

ResponseContentType = text/xml

Dim msXml As MemoryStream = New MemoryStream

Dim xmlWriter As XmlTextWriter = New XmlTextWriter(msXml EncodingUTF)

xmlWriterWriteStartElement(rss)

xmlWriterWriteAttributeString(version )

xmlWriterWriteStartElement(channel)

xmlWriterWriteElementString(title 【孟宪会之精彩世界】)

xmlWriterWriteElementString(link /Rssaspx)

xmlWriterWriteElementString(description NET开发技术)

xmlWriterWriteElementString(language zhCN)

xmlWriterWriteElementString(copyright Copyright 【孟宪会之精彩世界】)

xmlWriterWriteElementString(managingEditor amxh[AT])

xmlWriterWriteStartElement(image)

xmlWriterWriteElementString(title 【孟宪会之精彩世界】)

xmlWriterWriteElementString(width )

xmlWriterWriteElementString(height )

xmlWriterWriteElementString(link /)

xmlWriterWriteElementString(url _//gif)

xmlWriterWriteEndElement

xmlWriterWriteStartElement(item)

xmlWriterWriteElementString(title ASPNET 中直接将Access数据库导入到Excel文件中)

xmlWriterWriteElementString(description ASPNET 中直接将Access数据库导入到Excel文件中)

xmlWriterWriteElementString(link /article/abceecadc/readaspx)

xmlWriterWriteElementString(pubDate Sat Dec :: GMT)

xmlWriterWriteEndElement

xmlWriterWriteEndElement

xmlWriterWriteEndElement

xmlWriterFlush

msXmlPosition =

Dim xmlReader As XmlReader = XmlReaderCreate(msXml)

While xmlReaderRead

If xmlReaderName = rss Then

ResponseWrite(xmlReaderReadOuterXml)

End If

End While

ResponseEnd

msXmlClose

xmlWriterClose

xmlReaderClose

End Sub               

上一篇:HTML引用XML数据

下一篇:XML入门教程:XML有哪些用途?