急!!100分问一用C#写xml文件的问题
<?xml version= "1.0 " encoding= "utf-8 "?>
<objects name= "UpdateFileInfos " xmlns= "http://www.springframework.net ">
<object name= "AgileIMClient.exe ">
<property name= "URL " value= "http://192.168.1.195/AgileIMClient/AgileIMClient.exe " />
<property name= "Version " value= "2.0.0.0 " />
<property name= "FileType " value= ".exe " />
<property name= "IsValid " value= "true " />
</object>
<object name= "ESFramework.dll ">
<property name= "URL " value= "http://192.168.1.195/AgileIMClient/ESFramework.dll " />
<property name= "Version " value= "2.0.0.0 " />
<property name= "FileType " value= ".dll " />
<property name= "IsValid " value= "true " />
</object>
</objects>
我想再向其中添加一个object,如:
<object name= "DirectX.Capture.dll ">
<property name= "URL " value= "http://192.168.1.195/AgileIMClient/DirectX.Capture.dll " />
<property name= "Version " value= "2.0.0.0 " />
<property name= "FileType " value= ".dll " />
<property name= "IsValid " value= "true " />
</object>
用C#该怎么写???
[解决办法]
static void Main()
{
System.Xml.XmlDocument xd = new System.Xml.XmlDocument();
xd.Load( @ "o.xml " );
XmlElement oElem = xd.CreateElement( "object ");//新增记录会加上xmlns= " ",不知如何去掉
oElem.SetAttribute( "name ", "DirectX.Capture.dll ");
XmlElement pElem1 = xd.CreateElement( "property ");
pElem1.SetAttribute( "name ", "URL ");
pElem1.SetAttribute( "value ", "http://192.168.1.195/AgileIMClient/DirectX.Capture.dll ");
oElem.AppendChild(pElem1);
XmlElement pElem2 = xd.CreateElement( "property ");
pElem2.SetAttribute( "name ", "Version ");
pElem2.SetAttribute( "value ", "2.0.0.0 ");
oElem.AppendChild(pElem2);
XmlElement pElem3 = xd.CreateElement( "property ");
pElem3.SetAttribute( "name ", "FileType ");
pElem3.SetAttribute( "value ", ".dll ");
oElem.AppendChild(pElem3);
XmlElement pElem4 = xd.CreateElement( "property ");
pElem4.SetAttribute( "name ", "IsValid ");
pElem4.SetAttribute( "value ", "true ");
oElem.AppendChild(pElem4);
xd.DocumentElement.AppendChild(oElem);
xd.Save( @ "o.xml " ) ;
}
------解决方案--------------------
有人知道怎么去掉xmlns= " "吗?
楼上的代码有效,只是新增记录会加上xmlns= " ",如果使用DataGrid来显示XML时,会报错提示
未处理的“System.Data.DuplicateNameException”类型的异常出现在 system.data.dll 中。
其他信息: 名为“object”的 DataTable 已属于此数据集。
如果手动去掉再显示的话,就没有问题了,当然即使用xmlns= " ",在IE和FF浏览器中显示也没有问题
运行程序后得到的XML:
<?xml version= "1.0 " encoding= "utf-8 "?>
<objects name= "UpdateFileInfos " xmlns= "http://www.springframework.net ">
<object name= "AgileIMClient.exe ">
<property name= "URL " value= "http://192.168.1.195/AgileIMClient/AgileIMClient.exe " />
<property name= "Version " value= "2.0.0.0 " />
<property name= "FileType " value= ".exe " />
<property name= "IsValid " value= "true " />
</object>
<object name= "ESFramework.dll ">
<property name= "URL " value= "http://192.168.1.195/AgileIMClient/ESFramework.dll " />
<property name= "Version " value= "2.0.0.0 " />
<property name= "FileType " value= ".dll " />
<property name= "IsValid " value= "true " />
</object>
<object name= "DirectX.Capture.dll " xmlns= " ">
<property name= "URL " value= "http://192.168.1.195/AgileIMClient/DirectX.Capture.dll " />
<property name= "Version " value= "2.0.0.0 " />
<property name= "FileType " value= ".dll " />
<property name= "IsValid " value= "true " />
</object>
</objects>
用DataGrid显示XML
private void btnDisplay_Click(object sender, System.EventArgs e)
{
string XmlFilename;
XmlFilename = "F:\\AddXML\\MyAdd2.xml ";
DataSet objDataSet = new DataSet();
objDataSet.ReadXml(XmlFilename);
dataGrid1.DataSource = objDataSet.Tables[0].DefaultView;
}