0

我已经使用 c# 创建了一个 xml 文件。

XmlTextWriter writer = new XmlTextWriter("Product.xml", System.Text.Encoding.UTF8);
writer.WriteStartDocument(true);
writer.Formatting = Formatting.Indented;
writer.Indentation = 2;

然后,我创建我的字符串:

string stringXML = string.Empty;
stringXML = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><configurations xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"configurations.xsd\"><ProductsList><Product><ID>1</ID><Description>EPR</Description></Product></ProductsList></configurations>";

然后我想将我的stringXML写入Product.xml文件。

我试过了 :

System.IO.File.WriteAllText("Product.xml", stringXML);

但它没有用......

我怎样才能做到这一点?

4

1 回答 1

2

试试看

string s = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><configurations xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"configurations.xsd\"><ProductsList><Product><ID>1</ID><Description>EPR</Description></Product></ProductsList></configurations>";
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(s);
xdoc.Save("Product.xml");

更新

string name = saveFileDialog1.FileName;
string s = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><configurations xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"configurations.xsd\"><ProductsList><Product><ID>1</ID><Description>EPR</Description></Product></ProductsList></configurations>";
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(s);
xdoc.Save(name);

既然saveFileDialog1是你SaveFileDialog

于 2012-10-01T09:18:42.977 回答