读:
//打开某文件(假设webconfig在根目录中)
string filename=ServerMapPath("/") + @"WebApplication1\webconfig";
XmlDocument xmldoc= new XmlDocument();
xmldocLoad(filename);
XmlNodeList topM=xmldocDocumentElementChildNodes;
foreach(XmlElement element in topM)
{
if(elementNameToLower()=="appsettings")
{
//得到该节点的子节点
XmlNodeList nodelist=elementChildNodes;
if ( nodelistCount >0 )
{
//DropDownList1ItemsClear();
foreach(XmlElement el in nodelist)//读元素值
{
//DropDownList1ItemsAdd(elAttributes["key"]InnerXml);
//thisTextBox2Text=elAttributes["key"]InnerText;
thisTextBox2Text=elAttributes["key"]Value;
thisLabel1Text=elAttributes["value"]Value;
//同样在这里可以修改元素值,在后面save。
// elAttributes["value"]Value=thisTextBox2Text;
}
}
}
}
xmldocSave(filename);
在某节点下增加一个元素,并设置值:
if(elementNameToLower()=="appsettings")
{
XmlElement elem =xmldocCreateElement("add");
elementAppendChild(elem);
elemInnerText="ltp";
xmldocSave(filename);
}
效果:
<appSettings>
<add key="密码" value="admin" />
<add>ltp</< span="" />add>
</< span="" />appSettings>
在某节点下增加一个元素,并增加两个属性:
if(elementNameToLower()=="appsettings")
{
XmlElement elem =xmldocCreateElement("add");
elementAppendChild(elem);
XmlAttribute xa=xmldocCreateAttribute("key");
xaValue="ltp";
XmlAttribute xa2=xmldocCreateAttribute("value");
xa2Value="first";
elemSetAttributeNode(xa);
elemSetAttributeNode(xa2);
xmldocSave(filename);
}
效果:
<appSettings>
<add key="密码" value="admin" />
<add key="ltp" value="first" />
</< span="" />appSettings>
//添加空元素:
XmlNode node=docCreateElement(groupname);
nodeInnerText="";
docLastChildAppendChild(node);
docSave(xmlfile);
删除一个节点元素
string itemname=thislistBox1SelectedItemToString();
thislistBox1ItemsRemove(thislistBox1SelectedItem);
//begin del xmlfile
XmlDocument doc=new XmlDocument();
docLoad(xmlfile);
XmlNodeList topM=docDocumentElementChildNodes;
foreach(XmlElement element in topM)
{
if(elementName==thiscomboBox1Text)
{
//得到该节点的子节点
XmlNodeList nodelist=elementChildNodes;
foreach(XmlElement el in nodelist)//读元素值
{
if(elAttributes["key"]Value==itemname)
{
elementRemoveChild(el);
}
}//循环元素
}//得到组
}//循环组
docSave(xmlfile); //一定要保存一下,否则不起作用
//筛选数据
private void Reader_Xml(string pathFlie)
{
XmlDocument Xmldoc=new XmlDocument();
XmldocLoad(pathFlie);
XmlNodeList Record1=XmldocDocumentElementSelectNodes(Code[@id='1'])
int f=0;
foreach(XmlNode xnode in Record1)
{
}
}///////读取xml数据 两种xml方式/
<aaa>
<bb>something</< span="" />bb>
<cc>something</< span="" />cc>
</< span="" />aaa>
<aaa>
<add key="123" value="321"/>
</< span="" />aaa>
///////第一种方法/
DSReadXml("your xmlfile name");
ContainerDataItem("bb");
ContainerDataItem("cc");
DSReadXmlSchema("your xmlfile name");
///////第二种方法/
<aaa>
<add key="123" value="321"/>
</< span="" />aaa>
如果我要找到123然后取到321应该怎么写呢?
using SystemXML;
XmlDataDocument xmlDoc = new SystemXmlXmlDataDocument();
xmlDocLoad(@"c:\Configxml");
XmlElement elem = xmlDocGetElementById("add");
string str = elemAttributes["value"]Value
///////第三种方法: SelectSingleNode 读取两种格式的xml ---/
--------------------------------------------------------------------
Data Source=yf; user id=ctm_dbo;password=123
--------------------------------------------------------------------------
XmlDocument doc = new XmlDocument();
docLoad(strXmlName);
XmlNode node=docSelectSingleNode("/configuration/appSettings/ConnectionString");
if(node!=null)
{
string k1=nodeValue; //null
string k2=nodeInnerText;//Data Source=yf; user id=ctm_dbo;password=123
string k3=nodeInnerXml;//Data Source=yf; user id=ctm_dbo;password=123
node=null;
}
--------------------------------------------------------------------
XmlNode node=docSelectSingleNode("/configuration/appSettings/add");
if(node!=null)
{
string k=nodeAttributes["key"]Value;
string v=nodeAttributes["value"]Value;
node=null;
}
--------------------------------------------------------------------
XmlNode node=docSelectSingleNode("/configuration/appSettings/add");
if(node!=null)
{
XmlNodeReader nr=new XmlNodeReader(node);
nrMoveToContent();
//检查当前节点是否是内容节点。如果此节点不是内容节点,则读取器向前跳至下一个内容节点或文件结尾。
nrMoveToAttribute("value");
string s=nrValue;
node=null;
}
我晕,人家问代码怎么写,你们却答非所问
首先命名空间:
using SystemIO;
其次listbox添加内容,此步可跳过
listBox1ItemsAdd("A");
listBox1ItemsAdd("B");
listBox1ItemsAdd("C");
listBox1ItemsAdd("D");
listBox1ItemsAdd("E");
listBox1ItemsAdd("F");
最后,在FormClosing事件里添加保存的代码
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
FileStream fs = new FileStream("C:\\tmptxt", FileModeOpenOrCreate);
//保存路径可以自己更改
StreamWriter sw = new StreamWriter(fs);
foreach (object item in listBox1Items)
{
swWriteLine(itemToString());
}
swClose();
fsClose();
}
有事请HI我:)
为了使用列表框控件,首先需要向列表框控件中添加数据。在MFC类库中,列表框控件被封装为CListBox类。CListBox类提供了AddString方法向列表框中添加数据,该方法语法如下:
int AddString(LPCTSTR lpszItem);
参数说明如下。
lpszItem:表示向列表中添加的字符串数据。
返回值:表示新添加的数据在列表框中的索引位置。索引从零开始。
例如,下面的语句利用AddString方法向列表中添加数据。
m_DataListAddString("历史");
m_DataListAddString("地理");
m_DataListAddString("政治");
m_DataListAddString("生物");
m_DataListAddString("体育");
此外,还可以使用InsertString方法向列表框中添加数据,该方法语法如下:
int InsertString(int nIndex, LPCTSTR lpszItem);
参数说明如下。
þ nIndex:表示列表中的一个索引位置,方法将在该位置处添加数据,如果该参数为-1,则在列表框中数据的末尾位置添加。
þ lpszItem:表示向列表中添加的字符串数据。
返回值:表示新添加的数据在列表框中的索引位置。索引从零开始。
例如,下面的语句应用InsertString方法向列表框中添加数据。
m_DataListInsertString(0, "历史");
m_DataListInsertString(1, "地理");
m_DataListInsertString(2, "政治");
m_DataListInsertString(3, "生物");
m_DataListInsertString(4, "体育");
向列表框中添加数据
772 为列表框中的项目添加复选功能
在使用列表框控件时,有时需要利用列表框进行多项选择。这就需要列表框中的选项具有类似复选框的功能。那么如何让列表框中的数据以复选框的形式显示呢?可以按如下的方法实现。
例73 为列表框中的项目添加复选功能。
(1)创建一个基于对话框的工程,向对话框中添加列表框和按钮控件,如图749所示。
(2)利用类向导为列表框命名为m_DataList,类型为CListBox,如图750所示。
图749 对话框资源设计
图750 为列表框控件关联名称
(3)在对话框的头文件中将m_DataList的类型CListBox修改为CCheckListBox。
CCheckListBox m_DataList;
CCheckListBox派生于CListBox,它为数据项提供了复选功能。
(4)在列表框的属性窗口中设置Has strings属性,并设置Fixed自绘风格,如图751所示。
图751 设置列表框属性
(5)在对话框初始化时向列表框中添加数据。
m_DataListInsertString(0, "历史");
m_DataListInsertString(1, "地理");
m_DataListInsertString(2, "政治");
m_DataListInsertString(3, "生物");
m_DataListInsertString(4, "体育");
(6)处理“确定”按钮的单击事件,统计用户选择的学科信息。
void CCheckListDlg::OnConfirm()
{
int nItemCount = m_DataListGetCount(); //获取项目数量
CString szContent, szItemData; //定义字符串变量
szContent = "";
for (int i=0; i<nItemCount; i++) //遍历列表项
{
if (m_DataListGetCheck(i) == BST_CHECKED) //判断列表项是否被选中
{
m_DataListGetText(i, szItemData); //获取列表项文本
szContent += szItemData + "\r\n"; //记录列表项文本
}
}
if (!szContentIsEmpty())
{
MessageBox(szContent, "您选择的学科"); //显示用户选择的信息
}
}
(7)运行程序,效果如图752所示。
图752 统计列表框信息
773 同时选择多个项目
默认情况下,列表框控件中的项目只能同时选择一项,但在实际开发中,有时需要选择多项数据。为此,需要设置列表框的Selection属性为Multiple,如图753所示。
图753 列表框属性设置
这样,在列表框中即可选择多项数据,如图754所示。
图754 多项选择数据
string s = "你将前往:";
for (int i = 0; i < checkedListBox1ItemsCount; i++)
{
if (checkedListBox1GetItemChecked(i))//判断第i项是否被选中,选中的话就获取该项的值,把值串接到字符串s里。
{
s += checkedListBox1GetItemText(i)+"、";
}
}
MessageBoxShow(s);
你重复提问了
响应listbox的LBN_DBLCLK消息(就是双击消息)
在这个消息里面获取到选中的用户名
以这个用户名做索引,获取到此人的其他信息,比如(年龄,性别等)
然后定一个对话框,把这些值传入,再创建对话框即可:
CDlgInfo dlg;
dlgusername = "张三";
dlgage = "33";
dlgsex = "不男不女";
//必须把上面的值传给CDlgInfo上表示这些信息的响应控件的变量
dlgDoModal();
private void listBox1_DoubleClick(object sender, EventArgs e)
{
MessageBoxShow(thislistBox1SelectedItemToString());
}
控件的双击事件
可以先做个检测,先在末尾插入个代码:
function checkaa(listbox as listbox, text as string) as boolean
for i = 0 to (listboxlistcount - 1)
if listboxlist(i) = text then
checkaa = falseexit subend ifnextcheckaa = true
end function
然后在加项目时用checkaa函数验证一下,如果没重复,则返回true
参数listbox为要验证的列表框
参数text为要检查的字符
先在末尾插入个代码:
sub deleteitem(listbox as listbox)
listboxremoveitem listboxlistindexend sub然后在需要删除选择项时调用deleteitem就可以了,
参数listbox为要删除选择项的列表框
3在添加完成后要点击完成按钮后,需要把该窗口中的listbox列表中内容保存在ini文件中,还要把该窗口中的listbox列表中内容复制到主窗体的listbox列表中,并需要启动时将保存的文件名读取到主窗体的listbox列表中
可参照如下ini格式:[tip]歌曲数=2歌曲1=c:\东方红mp3
歌曲2=c:\第二曲mp3
读取时先取歌曲数,再用for x = l to u next这种语句
fornext语句样式:
for 记数用的变量名 = 起始数 to 终止数
以上就是关于C# 读取某节点的值 在线等。全部的内容,包括:C# 读取某节点的值 在线等。、在C#窗体应用程序中怎么在窗口关闭前保存ListBox中的内容代码是怎样的、VC++6.0环境下如何将需要的内容显示在列表框中具体一些。。谢谢~~等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)