将网络视图输出为 XML
本文关键字:XML 输出 视图 网络 | 更新日期: 2023-09-27 17:55:58
我在尝试让它工作时遇到麻烦。
我正在尝试实现的目标:使用CMD运行网络视图命令并将结果放入如下所示的XML文件中。
<Net_View>
<result id=1>
Andy's Computers.
</result>
<result id=2>
Chris PC
</result>
</Net_View>
很抱歉没有发布更多代码!!
protected static void Main(string[] args)
{
string output = string.Empty;
string error = string.Empty;
using (Process cmd = new Process())
{
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.Arguments = "/c Net View /all";
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.Start();
while (!cmd.StandardOutput.EndOfStream)
{
string line = cmd.StandardOutput.ReadLine();
if (line == "There are no entries in the list.")
return null;
if (line != string.Empty)
{
if (line[0] == "''")
}
}
using (StreamReader str = cmd.StandardOutput)
{
}
using (StreamReader str = cmd.StandardOutput)
{
error = str.ReadToEnd();
}
string SANG = cmd.StandardOutput.ReadToEnd();
cmd.Close();
}
}
结果代码:
using System;
using System.Diagnostics;
using System.Xml;
namespace XMLNet
{
class Program
{
static void Main(string[] args)
{
Console.Write("Executing cmd... ");
using (Process _cmd = new Process())
{
_cmd.StartInfo.FileName = "cmd.exe";
_cmd.StartInfo.Arguments = "/c Net View /all";
_cmd.StartInfo.CreateNoWindow = true;
_cmd.StartInfo.UseShellExecute = false;
_cmd.StartInfo.RedirectStandardOutput = true;
_cmd.Start();
using (XmlTextWriter _writer = new XmlTextWriter("net_view.xml", System.Text.Encoding.UTF8))
{
int _id = 0;
_writer.WriteStartDocument(true);
_writer.Formatting = Formatting.Indented;
_writer.Indentation = 2;
_writer.WriteStartElement("Net_View");
while (!_cmd.StandardOutput.EndOfStream)
{
string _line = _cmd.StandardOutput.ReadLine();
if (_line == "There are no entries in the list.")
return;
if (!String.IsNullOrWhiteSpace(_line))
{
if (_line.StartsWith(@"''"))
{
string _aux = _line.TrimStart('''').TrimEnd();
_writer.WriteStartElement("result");
_writer.WriteAttributeString("id", _id.ToString());
_writer.WriteString(_aux);
_writer.WriteEndElement();
_id++;
}
}
}
_writer.WriteEndElement();
_writer.WriteEndDocument();
_writer.Close();
}
}
Console.WriteLine("Done!");
}
}
}
它完全符合您的要求。