大家来总结。关于asp.net更新 xml 的操作,有几种,都有例子最好了.

skylen 2003-10-16 10:33:07
大家来总结。关于asp.net更新 xml 的操作,有几种,都有例子最好了.
...全文
89 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
好运 2003-10-17
  • 打赏
  • 举报
回复
Mark
skylen 2003-10-17
  • 打赏
  • 举报
回复
还有没有人跟了啊?
cnhgj 2003-10-17
  • 打赏
  • 举报
回复
using System.Xml;
cnhgj 2003-10-17
  • 打赏
  • 举报
回复
xmlDocument xmldoc = new xmlDocument();
xmldoc.Load(@"c:\test.xml");
xmldoc.GetElementsByTagName["abc"].value = "dfsafsa";
xmldoc.Save(@"c:\test.xml");
eagle_hb 2003-10-16
  • 打赏
  • 举报
回复
MAKR XML
saucer 2003-10-16
  • 打赏
  • 举报
回复
>>>关于asp.net更新 xml 的操作,

1. if your xml is two levels deep and very regular, use DataSet
2. if your xml is not very large, use DOM classes in System.Xml
3. if your xml is large and you only need to update some known nodes, use XmlTextReader to read through and update the specified nodes and write to a stream directly
rgbcn 2003-10-16
  • 打赏
  • 举报
回复
XmlReader和XmlTextReader类定义在System.XML名字空间中。

XmlTextReader类源于XmlReader类。XmlTextReader类可用来读取XML文档。此类的Read方法读取此文档,直到其节点结束。
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using System.Xml;

public class WebForm2 : Page
{
public StringBuilder outputQ;
public StringBuilder outputXml;
public DocumentNavigator nav = null;
public HtmlInputFile XmlFile;

public System.Web.UI.WebControls.Xml MyXml;

public System.Web.UI.WebControls.TextBox TextBox1;
public System.Web.UI.WebControls.TextBox TextBox2;
public System.Web.UI.WebControls.TextBox TextBox3;
public System.Web.UI.WebControls.Button Query;
public System.Web.UI.WebControls.Label FileLabel;

public void On_KeyUp(object sender, System.EventArgs e)
{
Response.Write("Works");
}

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//
// Evals true first time browser hits the page
//
}
}

public void Query_Click(object sender, System.EventArgs e)
{
HttpPostedFile xmlfile = XmlFile.PostedFile;
XmlDocument doc = new XmlDocument();
MyXml.Document = new XmlDocument();
// TextBox2.Text="";
// TextBox3.Text="";

if (xmlfile.FileName != String.Empty)
{
try
{
FileLabel.Text= xmlfile.FileName;

MyXml.Document.Load(xmlfile.FileName);
outputXml = new StringBuilder();
XmlTextReader reader = new XmlTextReader (xmlfile.FileName);
ShowDocument();
TextBox3.Text = outputXml.ToString();

outputQ = new StringBuilder();
doc.Load(xmlfile.FileName);
DocumentNavigator nav = new DocumentNavigator(doc);
// Perform the query e.g. "descendant::book/price"
XPathQuery(nav, TextBox1.Text);
TextBox2.Text = outputQ.ToString();

}
catch (Exception exp) {
//outputQ.Append("</xmp><font color=\"#FF6600\">"+ exp.Message+"</font><xmp>");
}
finally {}
}
else if (FileLabel.Text != String.Empty)
{
try
{
MyXml.Document.Load(FileLabel.Text);
outputXml = new StringBuilder();
XmlTextReader reader = new XmlTextReader (FileLabel.Text);
ShowDocument();
TextBox3.Text = outputXml.ToString();

ShowDocument();

outputQ = new StringBuilder();
doc.Load(FileLabel.Text);
DocumentNavigator nav = new DocumentNavigator(doc);
// Perform the query e.g. "descendant::book/price"
XPathQuery(nav, TextBox1.Text);
TextBox2.Text = outputQ.ToString();

}
catch (Exception exp) {
outputQ.Append("</xmp><font color=\"#FF6600\">"+ exp.Message+"</font><xmp>");
}
finally {}
}
}

private void XPathQuery(XmlNavigator navigator, String xpathexpr )
{
try
{
// Save context node position
navigator.PushPosition();
navigator.Select (xpathexpr);
FormatXml(navigator);

// Restore context node position
navigator.PopPosition();
}
catch (Exception e)
{
}
}

//***************************** Navigator ************************************
private void FormatXml (XmlNavigator navigator)
{
while (navigator.MoveToNextSelected())
{
switch (navigator.NodeType)
{
case XmlNodeType.ProcessingInstruction:
Format (navigator, "ProcessingInstruction");
break;
case XmlNodeType.DocumentType:
Format (navigator, "DocumentType");
break;
case XmlNodeType.Document:
Format (navigator, "Document");
break;
case XmlNodeType.Comment:
Format (navigator, "Comment");
break;
case XmlNodeType.Element:
Format (navigator, "Element");
break;
case XmlNodeType.Text:
Format (navigator, "Text");
break;
case XmlNodeType.Whitespace:
Format (navigator, "Whitespace");
break;
}
}
outputQ.Append("\r\n");
}

// Format the output
private void Format (XmlNavigator navigator, String NodeType)
{
String value = String.Empty;
String name = String.Empty;

if (navigator.HasChildren)
{
name = navigator.Name;
navigator.MoveToFirstChild();
if (navigator.HasValue)
{
value = navigator.Value;
}
}
else
{
if (navigator.HasValue)
{
value = navigator.Value;
name = navigator.Name;
}
}
outputQ.Append(NodeType + "<" + name + ">" + value);
outputQ.Append("\r\n");
}

// ********************************** XmlReader *****************************
public void ShowDocument ()
{
outputXml = new StringBuilder();
XmlTextReader reader = new XmlTextReader (FileLabel.Text);

while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.ProcessingInstruction:
Format (reader, "ProcessingInstruction");
break;
case XmlNodeType.DocumentType:
Format (reader, "DocumentType");
break;
case XmlNodeType.Comment:
Format (reader, "Comment");
break;
case XmlNodeType.Element:
Format (reader, "Element");
break;
case XmlNodeType.Text:
Format (reader, "Text");
break;
case XmlNodeType.Whitespace:
break;
}
}
TextBox3.Text = outputXml.ToString();
}

protected void Format(XmlReader reader, String NodeType)
{
// Format the output
for (int i=0; i < reader.Depth; i++)
{
outputXml.Append('\t');
}

outputXml.Append(reader.Prefix + NodeType + "<" + reader.Name + ">" + reader.Value);

// Display the attributes values for the current node
if (reader.HasAttributes)
{
outputXml.Append(" Attributes:");

for (int j=0; j < reader.AttributeCount; j++)
{
outputXml.Append(reader[j]);
}
}
outputXml.Append("\r\n");
}

/// ************************* DOM *********************************
protected void ShowDocument(XmlNode node)
{
if (node != null)
Format (node);

if (node.HasChildNodes)
{
node = node.FirstChild;
while (node != null)
{
ShowDocument(node);
node = node.NextSibling;
}
}
}

// Format the output
private void Format (XmlNode node)
{
if (!node.HasChildNodes)
{
outputXml.Append("\t" + "<" + node.Value + ">");
}

else
{
outputXml.Append("<" + node.Name + ">");
if (XmlNodeType.Element == node.NodeType)
{
XmlNamedNodeMap map = node.Attributes;
foreach (XmlNode attrnode in map)
outputXml.Append(" " + attrnode.Name + "<" + attrnode.Value + "> ");
}
outputXml.Append("\r\n");
}
}
}


下面就是webform2.aspx了
webform2.aspx
-----------------------------------
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Assembly Name="System.Xml" %>
<%@ Import Namespace="System.Xml" %>
<%@ Page Language="C#" Inherits="WebForm2" Src="WebForm2.cs" Debug="true" %>

<HTML><HEAD>

<script runat="server" language="C#">
// Put page script here
public void On_KeyUp(object sender, System.EventArgs e)
{
Response.Write("Works");
}

</script>

<!--<link REL="STYLESHEET" HREF="default.css" TYPE="text/css">-->
<TITLE>test</TITLE>
</HEAD>

<BODY >


<form method="post" action="WebForm2.aspx" runat="server" enctype="multipart/form-data">

<div align="left">
<table>
<tr>
<td>XML Document:</td>
<td><input type=file id="XmlFile" runat=server> FileName:</td>
<td><asp:label id="FileLabel" runat="server"></asp:label></td>
</tr>

<tr>
<td>XPath Expression</td>
<td><asp:textbox id=TextBox1 runat="server" Height="20" Width="300" text=".//text()" OnKey_Up="On_KeyUp"></asp:textbox></td>
<td><asp:button type=submit OnClick="Query_Click" runat="server" Height="20" Width="91" text="Query"></asp:button></td>
</tr>
</table>

</br>
<table>
<tr><td>Output from Query</td><td>XML Data</td><tr>
<tr><td>Query Display: <asp:dropdownlist runat="server">
<asp:listitem>Descriptive</asp:listitem>
<asp:listitem>XML</asp:listitem>
</asp:dropdownlist>
</td><tr>
<tr>
<td width="50%" valign="top" align="left"><asp:textbox id=TextBox2 runat="server" Height="400" Width="350" TextMode="MultiLine" Rows="10"></asp:textbox></td>
<td width="50%" valign="top" align="left"><asp:xml id="MyXml" transformsource="test.xsl" runat=server/></asp:xml></td>
</tr>
</table>
</div>

<td><asp:textbox id=TextBox3 runat="server" Height="1" Width="5" TextMode="MultiLine" Rows="110"></asp:textbox></td>
</form>
</BODY>
</HTML>

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧