日期:2014-05-18  浏览次数:20596 次

读写xml问题
a.xml
<n>
<name>ttttt</name>
<name>ppppp</name>
</n>
现在想要在后台把用户名写入这个xml(如string name="oooooo")
然后用js读出所有用户名付给数组(是大量的).
不知如何做请高手帮忙

------解决方案--------------------
用户名写入:
//先读取xml文件
FileStream xmlfilein;
xmlfilein=new FileStream(Server.MapPath("a.xml "),FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
DataSet ds=new DataSet();
ds.ReadXml(xmlfilein,XmlReadMode.ReadSchema);
xmlfilein.close();
//写入xml文件
DataRow newrow=ds.Tables["n"].NewRow();
newrow["name"]="oooooo";
ds.Tables["n"].Rows.Add(newrow);
FileStream xmlfileout;
xmlfileout=new FileStream(Server.MapPath("a.xml "),FileMode.Open,FileAccess.Write,FileShare.ReasWrite);
ds.WriteXml(xmlfileout,XmlReadMode.WriteSchema);
xmlfileout.close();

js中读取用户名,并赋值给数组:
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest()
{
if(window.ActiveXObject)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp=new XMLHttpRequest()
}
}

function startRequest()
{
createXMLHttpRequest();
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.open("get","a.xml",true);
xmlHttp.send(null);
}
function handleStateChange()
{
var item;
var xmlDoc=xmlHttp.responseXML;
items = xmlDoc.selectNodes("//name/Table");
var n = items.nextNode().selectSingleNode("//name").nodeTypedValue;
var arr=a.split(" ");
var arr1;
for(var i=0;i<a.length;i++)
     {
     arr1[i]=arr[i].value
     }
}
</script>