<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Model" namespace="Model">
<class name ="Model.UserInfo,Model" table="BIWEILUN">
<id name="ID" column="ID" type="Int32" unsaved-value="0">
<generator class ="increment"> </generator>
</id>
<property name="UserID" column ="UserID" type="string" length="20" not-null="false"/>
<property name="UserName" column ="UserName" type="string" length="20" not-null="false"/>
<property name="PassWord" column ="PassWord" type="string" length="20" not-null="false"/>
<property name="SafeCode" column ="SafeCode" type="string" length="20" not-null="false"/>
<property name="Question1" column ="Question1" type="string" length="20" not-null="false"/>
<property name="Answer1" column ="Answer1" type="string" length="20" not-null="false"/>
<property name="Question2" column ="Question2" type="string" length="20" not-null="false"/>
<property name="Answer2" column ="Answer2" type="string" length="20" not-null="false"/>
<property name="Mail" column="Mail" type="string" length="20" not-null="false" />
<property name="TrueName" column ="TrueName" type="string" length="20" not-null="false"/>
<property name="Sex" column ="Sex" type="string" length="4" not-null="false"/>
<property name="PersonID" column ="PersonID" type="string" length="30" not-null="false"/>
<property name="Job" column ="Job" type="string" length="20" not-null="false"/>
<property name="PlaceFrom" column ="PlaceFrom" type="string" length="10" not-null="false"/>
<property name="Role" column ="Role" type="string" length="10" not-null="false"/>
</class>
</hibernate-mapping>
XML文件如上,hibernate-mapping是根,下面有1个Child——class,class下面有一个ID的子结点,和很多property的子节点我现在只写出了这样的代码:
string xmlPath = Server.MapPath("~/User.xml");
XmlDocument xDoc = new XmlDocument();
xDoc.Load(xmlPath);
XmlNode root = xDoc.DocumentElement;
XmlNode classnode = root.ChildNodes[0];
XmlNodeList propertyList = classnode.ChildNodes;以上四句都能成功得到我想要的结构,但是propertyList得到的是包含ID的class下面的所有子节点,如果我用
XmlNodeList propertyList2 = classnode.SelectNodes("property");
却不能取到所有的property。是不是SelectNodes只对这样的XML格式有效果?
<propert>
<name>UserID</name>
<column>UserID</column>
...
</propert>我现在就是要向所有的property后添加
<property name="xxx" column ="xxx" type="string" length="10" not-null="false"/>
这样的,怎么做?
解决方案 »
- C# 中国象棋网络版源代码
- 把datatable数据批量导入到access数据库
- DBF文件求助:留下需要的,删除不需要的数据
- ListView控件怎么设置可以单击某行时选中该行前的复选框
- 怎样用C#设置共享指定的文件夹
- [向.net提问] 100分相送!!!winform中 怎么使用树的增加,刷新等细节方面的问题。求代码.
- C#.net中定制控件在编缉状态是为什么老是显示“创建控件时出错”,但运行后又可以正常显示呢
- .NET C#中能不能调用C++的Dll?怎么调用?
- 很简单的问题,但是我找不到
- 下面的代码是什么意思?(C# & XML)
- 两个radiobutton,点第一个,datagridview显示表a 的数据,点第二个,datagridview显示表b的数据。谢谢
- 关于线程的一个奇怪问题?求助!
XmlDocument xDoc = new XmlDocument();
xDoc.Load(xmlPath);
XmlNode root = xDoc.DocumentElement;
XmlNode classnode = root.ChildNodes[0]; XmlNamespaceManager nsmgr = new XmlNamespaceManager(xDoc.NameTable);
nsmgr.AddNamespace("nh2.2", "urn:nhibernate-mapping-2.2"); XmlNodeList propertyList = classnode.SelectNodes("nh2.2:property", nsmgr);
以下例子在user.xml中增加了一个时间属性,如果用Xpath将会更加方便。using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
XmlDocument xml = new XmlDocument();
xml.Load("User.xml");
XmlNodeList nodelist = xml.GetElementsByTagName("property"); foreach (XmlNode node in nodelist)
{
XmlElement element = (XmlElement)node;
//增加时间属性
element.SetAttribute("time", DateTime.Now.ToString());
Console.WriteLine(element.GetAttribute("time").ToString());
} //保存xml
xml.Save("User.xml"); Console.ReadKey();
}
}
}
<property name="UserID" column ="UserID" type="string" length="20" not-null="false"/>
<property name="UserName" column ="UserName" type="string" length="20" not-null="false"/>
<property name="PassWord" column ="PassWord" type="string" length="20" not-null="false"/>我想再加上一行格式差不多的,变成<property name="UserID" column ="UserID" type="string" length="20" not-null="false"/>
<property name="UserName" column ="UserName" type="string" length="20" not-null="false"/>
<property name="PassWord" column ="PassWord" type="string" length="20" not-null="false"/>
<property name="xxx" column ="xxx" type="string" length="20" not-null="false"/>只是对这种格式的XML操作不会
对
<propert>
<name>UserID</name>
<column>UserID</column>
...
</propert>这种格式的我会,用XMLNodeList就行了
XmlDocument doc = new XmlDocument();
doc.Load("test.xml");
XmlNodeList nodes = doc.SelectNodes("/*[local-name()='hibernate-mapping']/*[local-name()='class']/*[local-name()='property']");
string xmlPath = Server.MapPath("~/User.xml");
XmlDocument xDoc = new XmlDocument();
xDoc.Load(xmlPath);
XmlNode root = xDoc.DocumentElement;
XmlNode classnode = root.ChildNodes[0];
XmlNodeList nodes = classnode.SelectNodes("/*[local-name()='property']");//这样就取不到了
取到了还有插入的问题,怎么向后面插入
<property name="xxx" column ="xxx" type="string" length="20" not-null="false"/>
这种格式的呢?
给你解释:
/第1层名称=hibernate-mapping的所有节点/第2层名称=class的所有节点/第3层名称=property的所有节点注意层次关系. 一个斜杠表示1层。具体可参照 Xpath 语法----------------
classnode.SelectNodes("/*[local-name()='property']");//这样就取不到了
你这个意思是 取 第一层名称=property的所有节点 ,因为第一层没有名称为property的节点。 所以你取出来的是空的。----------取到了还有插入的问题,怎么向后面插入
<property name="xxx" column ="xxx" type="string" length="20" not-null="false"/>
这种格式的呢?
--------------------------稍后给代码
{
XmlDocument doc = new XmlDocument();
doc.Load("test.xml");
XmlNodeList nodes = doc.SelectNodes("/*[local-name()='hibernate-mapping']/*[local-name()='class']/*[local-name()='property']");
if (nodes.Count > 0 && nodes[0].ParentNode !=null)
{
XmlNode xnNew = nodes[0].Clone();
nodes[0].ParentNode.AppendChild(xnNew);
//怎么修改 xnNew 不需要说了吧?
}
doc.Save("test_out.xml");
}
<property name="xxx" column ="xxx" type="string" length="20" not-null="false"/> 这种格式的XMLNode?或是XmlElement?我感觉这个好像不是XmlNode初学者请大哥不吝赐教,一直在搞C++的,刚转型....
{
XmlDocument doc = new XmlDocument();
doc.Load("test.xml");
XmlNodeList nodes = doc.SelectNodes("/*[local-name()='hibernate-mapping']/*[local-name()='class']/*[local-name()='property']");
if (nodes.Count > 0 && nodes[0].ParentNode !=null)
{
XmlNode xnNew = nodes[0].Clone();
nodes[0].ParentNode.AppendChild(xnNew);
//怎么修改 xnNew 不需要说了吧?
XmlElement xe = doc.CreateElement("","property","urn:nhibernate-mapping-2.2");
XmlAttribute xa = doc.CreateAttribute("name");
xa.Value="role3";
xe.Attributes.Append(xa);
nodes[0].ParentNode.AppendChild(xe);
}
doc.Save("test_out.xml");
}
我是这么取到所有property的XmlNodeList的,而且确实取到了XmlNamespaceManager nsmgr = new XmlNamespaceManager(xDoc.NameTable);
nsmgr.AddNamespace("root", "urn:nhibernate-mapping-2.2");XmlNodeList propertyList = root.SelectNodes("/root:hibernate-mapping/root:class/root:property", nsmgr);
我现在要把他们从XML中抹掉该怎么办?就是把<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Model" namespace="Model">
<class name ="Model.UserInfo,Model" table="BIWEILUN">
<id name="ID" column="ID" type="Int32" unsaved-value="0">
<generator class ="increment"> </generator>
</id>
<property name="UserID" column ="UserID" type="string" length="20" not-null="false"/>
<property name="UserName" column ="UserName" type="string" length="20" not-null="false"/>
<property name="PassWord" column ="PassWord" type="string" length="20" not-null="false"/>
<property name="SafeCode" column ="SafeCode" type="string" length="20" not-null="false"/>
<property name="Question1" column ="Question1" type="string" length="20" not-null="false"/>
<property name="Answer1" column ="Answer1" type="string" length="20" not-null="false"/>
<property name="Question2" column ="Question2" type="string" length="20" not-null="false"/>
<property name="Answer2" column ="Answer2" type="string" length="20" not-null="false"/>
<property name="Mail" column="Mail" type="string" length="20" not-null="false" />
<property name="TrueName" column ="TrueName" type="string" length="20" not-null="false"/>
<property name="Sex" column ="Sex" type="string" length="4" not-null="false"/>
<property name="PersonID" column ="PersonID" type="string" length="30" not-null="false"/>
<property name="Job" column ="Job" type="string" length="20" not-null="false"/>
<property name="PlaceFrom" column ="PlaceFrom" type="string" length="10" not-null="false"/>
<property name="Role" column ="Role" type="string" length="10" not-null="false"/>
</class>
</hibernate-mapping>
变成<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Model" namespace="Model">
<class name ="Model.UserInfo,Model" table="BIWEILUN">
<id name="ID" column="ID" type="Int32" unsaved-value="0">
<generator class ="increment"> </generator>
</id>
</class>
</hibernate-mapping>
这样的效果我是这么做的 for(int i=0;i<propertyList.Count;i++)
propertyList[i].RemoveAll();结果效果却是这样的:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Model" namespace="Model">
<class name="Model.UserInfo,Model" table="BIWEILUN">
<id name="ID" column="ID" type="Int32" unsaved-value="0">
<generator class="increment">
</generator>
</id>
<property />
<property />
<property />
<property />
<property />
<property />
<property />
<property />
<property />
<property />
<property />
<property />
<property />
<property />
<property />
</class>
</hibernate-mapping>
private void button1_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load("test.xml");
XmlNodeList nodes = doc.SelectNodes("/*[local-name()='hibernate-mapping']/*[local-name()='class']/*[local-name()='property']");
if (nodes.Count > 0 && nodes[0].ParentNode != null)
{
XmlNode xnNew = nodes[0].Clone();
nodes[0].ParentNode.AppendChild(xnNew);
//怎么修改 xnNew 不需要说了吧?
XmlElement xe = doc.CreateElement("", "property", "urn:nhibernate-mapping-2.2");
XmlAttribute xa = doc.CreateAttribute("name");
xa.Value = "role3";
xe.Attributes.Append(xa);
nodes[0].ParentNode.AppendChild(xe);
}
doc.Save("test_out.xml"); //remove
nodes = doc.SelectNodes("/*[local-name()='hibernate-mapping']/*[local-name()='class']/*[local-name()='property']"); foreach (XmlNode xn in nodes)
{
if (xn.ParentNode != null)
{
xn.ParentNode.RemoveChild(xn);
}
} doc.Save("test_out_2.xml"); }
XmlElement xe = xDoc.CreateElement("", "property", "urn:nhibernate-mapping-2.2");
XmlAttribute[] xa = new XmlAttribute[5]; xa[0] = xDoc.CreateAttribute("name");
xa[0].Value = "role3";
xa[1] = xDoc.CreateAttribute("column");
xa[1].Value = "role3";
xa[2] = xDoc.CreateAttribute("type");
xa[2].Value = "string";
xa[3] = xDoc.CreateAttribute("length");
xa[3].Value = "20";
xa[4] = xDoc.CreateAttribute("not-null");
xa[4].Value = "false";
for (int i = 0; i < 5; i++)
xe.Attributes.Append(xa[i]);这样创建一个
<property name="Role" column ="Role" type="string" length="10" not-null="false"/>
这样的节点,代码会不会多了点?有没有好的 精简 写法?一下子5个属性全进去?感觉写的很繁琐...
//得到父节点
XmlNode root = xmlFile.SelectSingleNode(xpath);
//创建一个新的节点
XmlElement selfElement = xmlFile.CreateElement("property");
//设置节点属性
selfElement.SetAttribute("name", "test");
selfElement.SetAttribute("column", "col");
//将节点挂到其父节点上
root.AppendChild(selfElement);大概是以上这样子了,你调一下就可以了