我正在开发 C# 应用程序,但在输入 xml 文件时遇到问题。让我先显示代码:
Company comp = new Company();
comp.CompanyID = comboBox1.SelectedValue.ToString();
comp.CompanyName = comboBox1.Text;
comp.Serial = strEncryptedData;
comp.ListProduct = ll;
XmlDocument xDoc = new XmlDocument();
using (StringWriter stringWriter = new StringWriter())
{
XmlSerializer serializer = new XmlSerializer(typeof(Company));
serializer.Serialize(stringWriter, comp);
xDoc.LoadXml(stringWriter.ToString());
}
string temp = xDoc.OuterXml;
MessageBox.Show(temp);
System.IO.StreamWriter sw = new System.IO.StreamWriter(@"c:\test.xml");
sw.WriteLine(temp);
sw.Flush();
sw.Close();
程序写入文件,但是当我尝试以 xml 格式打开它时,我收到空白文档,里面什么都没有。当我在文本编辑器中打开它时,我收到了这个:
<?xml version="1.0" encoding="utf-16"?><CompanyXml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><CompanyName /><CompanyID>100</CompanyID><Serial>00000G2SB4BER9PSFJİ2GTVM2UC1VYEİ</Serial></CompanyXml>
这是我收到的正确数据,但无法以 xml 格式打开。
我怎样才能格式化它?还是我在写的时候做错了什么?