2012-10-01 2 views
0

나는 C#을 사용하여 xml 파일을 만들었습니다.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);

...

가 어떻게 이렇게 할 수 있습니까?

+0

당신의 동의 비율을 증가하십시오,이에 따라 http://stackoverflow.com/faq# howtoask – Usman

답변

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"); 

업데이트로 시도saveFileDialog1 이후

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); 

이 당신의 SaveFileDialog

+0

그럼 SaveFileDialog를 사용하여 파일을 저장하지만 빈 파일은 저장합니다 ... – user1051434

+0

SaveFileDialog를 사용하는 이유 xdoc.Save에서 올바른 경로를 지정하면 파일이 해당 위치에 저장됩니다. – Usman

+0

사용자가 원하는 경로를 선택해야하므로 사용자 saveFiledialog가 필요합니다. – user1051434

관련 문제