从c#应用程序中编写XML文档?
我有一个收集系统信息的C#程序。所有这些都是对xml文件的完整execept写入。我已将从c#应用程序中编写XML文档?,c#,xml,visual-studio-2010,iis,C#,Xml,Visual Studio 2010,Iis,我有一个收集系统信息的C#程序。所有这些都是对xml文件的完整execept写入。我已将系统版本、IE版本和Regedit版本转换为“ints”和“strings”。现在我遇到的问题是将输出写入两个文件: Sys_info.xml-程序生成的系统信息 unattend.xml-用于IIS设置,它使用程序中的变量 IIS无人参与.xml可在以下位置找到: 可悲的是,我不是一个程序员,而写这篇文章的人正在度假 不管怎样,下面是unattended.xml的xml代码: <?xml versi
系统版本、IE版本和Regedit
版本转换为“ints”和“strings”。现在我遇到的问题是将输出写入两个文件:
Sys_info.xml
-程序生成的系统信息unattend.xml
-用于IIS设置,它使用程序中的变量<?xml version="1.0" ?>
<unattend xmlns="urn:schemas-microsoft-com:unattend"
xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State">
<servicing>
<!-- Install a selectable update in a package that is in the Windows Foundation namespace -->
<package action="configure">
<assemblyIdentity
name="Microsoft-Windows-Foundation-Package"
version="6.0.5308.6" ***reg version int from registry goes here(from my program)***
language="neutral"
processorArchitecture="x86" **** Processor architecture goes here(from my program)****
publicKeyToken="31bf3856ad364e35"
versionScope="nonSxS"
/>
<selection name="IIS-WebServerRole" state="true"/>
<selection name="IIS-WebServer" state="true"/>
<selection name="IIS-CommonHttpFeatures" state="true"/>
<selection name="IIS-StaticContent" state="true"/>
<selection name="IIS-DefaultDocument" state="true"/>
<selection name="IIS-DirectoryBrowsing" state="true"/>
<selection name="IIS-HttpErrors" state="true"/>
<selection name="IIS-HttpRedirect" state="true"/>
<selection name="IIS-ApplicationDevelopment" state="true"/>
<selection name="IIS-ASPNET" state="true"/>
<selection name="IIS-NetFxExtensibility" state="true"/>
<selection name="IIS-ASP" state="true"/>
<selection name="IIS-CGI" state="true"/>
<selection name="IIS-ISAPIExtensions" state="true"/>
<selection name="IIS-ISAPIFilter" state="true"/>
<selection name="IIS-ServerSideIncludes" state="true"/>
<selection name="IIS-HealthAndDiagnostics" state="true"/>
<selection name="IIS-HttpLogging" state="true"/>
<selection name="IIS-LoggingLibraries" state="true"/>
<selection name="IIS-RequestMonitor" state="true"/>
<selection name="IIS-HttpTracing" state="true"/>
<selection name="IIS-CustomLogging" state="true"/>
<selection name="IIS-ODBCLogging" state="true"/>
<selection name="IIS-Security" state="true"/>
<selection name="IIS-BasicAuthentication" state="true"/>
<selection name="IIS-WindowsAuthentication" state="true"/>
<selection name="IIS-DigestAuthentication" state="true"/>
<selection name="IIS-ClientCertificateMappingAuthentication" state="true"/>
<selection name="IIS-IISCertificateMappingAuthentication" state="true"/>
<selection name="IIS-URLAuthorization" state="true"/>
<selection name="IIS-RequestFiltering" state="true"/>
<selection name="IIS-IPSecurity" state="true"/>
<selection name="IIS-Performance" state="true"/>
<selection name="IIS-HttpCompressionStatic" state="true"/>
<selection name="IIS-HttpCompressionDynamic" state="true"/>
<selection name="IIS-WebServerManagementTools" state="true"/>
<selection name="IIS-ManagementConsole" state="true"/>
<selection name="IIS-ManagementScriptingTools" state="true"/>
<selection name="IIS-ManagementService" state="true"/>
<selection name="IIS-IIS6ManagementCompatibility" state="true"/>
<selection name="IIS-Metabase" state="true"/>
<selection name="IIS-WMICompatibility" state="true"/>
<selection name="IIS-LegacyScripts" state="true"/>
<selection name="IIS-LegacySnapIn" state="true"/>
<selection name="IIS-FTPPublishingService" state="true"/>
<selection name="IIS-FTPServer" state="true"/>
<selection name="IIS-FTPManagement" state="true"/>
<selection name="WAS-WindowsActivationService" state="true"/>
<selection name="WAS-ProcessModel" state="true"/>
<selection name="WAS-NetFxEnvironment" state="true"/>
<selection name="WAS-ConfigurationAPI" state="true"/>
</package>
</servicing>
</unattend>
这是你能做的最便宜的事情
StringBuilder sb = new StringBuilder();
sb.Append("<?xml version=\'1.0\' ?>");
sb.Append("<unattend xmlns='urn:schemas-microsoft-com:unattend' xmlns:wcm='http://schemas.microsoft.com/WMIConfig/2002/State'>");
sb.Append("<servicing>");
sb.Append("<package action='configure'>");
sb.Append(" <assemblyIdentity");
sb.Append(" name='Microsoft-Windows-Foundation-Package'");
sb.Append(" version='6.0.5308.6' ");
sb.Append(" language='neutral'");
sb.Append(" processorArchitecture='x86' ");
sb.Append(" publicKeyToken='31bf3856ad364e35'");
sb.Append(" versionScope='nonSxS'");
sb.Append(" />");
foreach(var kvp in dictionaryOfValues)
{
sb.AppendFormat("<selection name='{0}' state='{1}'/>", kvp.Key, kvp.Value);
}
sb.Append("</package>");
sb.Append("</servicing>");
sb.Append("</unattend>");
// Write the string to a file.
System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\test.xml");
file.WriteLine(sb.ToString);
file.Close();
StringBuilder sb=新建StringBuilder();
某人加上(“”);
某人加上(“”);
某人加上(“”);
某人加上(“”);
某人加上(“”);
foreach(字典中的var kvp值)
{
sb.AppendFormat(“”,kvp.Key,kvp.Value);
}
某人加上(“”);
某人加上(“”);
某人加上(“”);
//将字符串写入文件。
System.IO.StreamWriter file=new System.IO.StreamWriter(“c:\\test.xml”);
文件写入行(sb.ToString);
file.Close();
当然,您可以将其解析为XmlReaders和XMLWriter,但是如果您希望在不需要学习太多的情况下完成这项工作,那么这段代码应该可以帮助您摆脱麻烦。它假定您有一个用于选择标记的键值字典。如果不这样做,只需为每一行添加一行,并使用“附加格式”将正确的值粘贴到其中
当孩子们周一来的时候,他们会帮你清理:)Hm.如果你是个程序员,我想让你看看微软的。你能给我一些简单的参考资料或一个例子吗?我真的要完成这个。MSDN的例子太难了implement@MrLister:这只是意味着-使用XDocument比XmlWriter简单得多。@user1235546:老实说,如果你不是一名程序员,那么你很难在短时间内学会足够多的C语言。你身边根本没有开发人员吗?事实上我有,但他们要到星期一才会在办公室。我对“C”有基本的了解,我希望能即兴创作。顺便说一句,XDocument到底是什么。