[xml][.NET]如何用C#程式碼產生xml文件

  • 11015
  • 0

[xml][.NET]如何用C#程式碼產生xml文件

產生XML並存成檔案test.xml並且輸出xml到字串變數:

XmlDocument doc = new XmlDocument();

//建立<?xml version="1.0" encoding="UTF-8"?>
//ps.看需求,其實可以不建
XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(declaration);

//建立<?MyCustomNameHere attribute1="val1" attribute2="val2"?>
//ps.同樣看需求,其實可以不建
XmlProcessingInstruction pi = doc.CreateProcessingInstruction("MyCustomNameHere", "attribute1=\"val1\" attribute2=\"val2\"");
doc.AppendChild(pi);

//建立包含namespace的節點
//<foo:bar xmlns:foo="http://tempuri.org/foo" />
//ps.還是看需求,其實可以不建
XmlElement nspace = doc.CreateElement(
	"foo", "bar", "http://tempuri.org/foo");
doc.AppendChild(nspace);

//建立子節點
XmlElement company = doc.CreateElement("Company");
nspace.AppendChild(company);
//建立子節點
XmlElement department = doc.CreateElement("Department");
department.SetAttribute("部門", "程式部");//設定屬性
department.SetAttribute("部門經理", "鹽巴");//設定屬性
									  //加入至company節點底下
company.AppendChild(department);

XmlElement members = doc.CreateElement("Members");//建立節點
												  //加入至department節點底下
department.AppendChild(members);

XmlElement info = doc.CreateElement("Information");
info.SetAttribute("姓名", "鹽巴");
info.SetAttribute("電話", "3939889");
//加入至members節點底下
members.AppendChild(info);
info = doc.CreateElement("Information");
info.SetAttribute("姓名", "雷神所爾");
info.SetAttribute("電話", "8825251");
//加入至members節點底下
members.AppendChild(infXmlDocument doc = new XmlDocument();

//建立<?xml version="1.0" encoding="UTF-8"?>
//ps.看需求,其實可以不建
XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(declaration);

//建立<?MyCustomNameHere attribute1="val1" attribute2="val2"?>
//ps.同樣看需求,其實可以不建
XmlProcessingInstruction pi = doc.CreateProcessingInstruction("MyCustomNameHere", "attribute1=\"val1\" attribute2=\"val2\"");
doc.AppendChild(pi);

//建立包含namespace的節點
//<foo:bar xmlns:foo="http://tempuri.org/foo" />
//ps.還是看需求,其實可以不建
XmlElement nspace = doc.CreateElement(
	"foo", "bar", "http://tempuri.org/foo");
doc.AppendChild(nspace);

//建立根節點
XmlElement company = doc.CreateElement("Company");
nspace.AppendChild(company);
//建立子節點
XmlElement department = doc.CreateElement("Department");
department.SetAttribute("部門", "程式部");//設定屬性
department.SetAttribute("部門經理", "鹽巴");//設定屬性
									  //加入至company節點底下
company.AppendChild(department);

XmlElement members = doc.CreateElement("Members");//建立節點
												  //加入至department節點底下
department.AppendChild(members);

XmlElement info = doc.CreateElement("Information");
info.SetAttribute("姓名", "鹽巴");
info.SetAttribute("電話", "3939889");
//加入至members節點底下
members.AppendChild(info);
info = doc.CreateElement("Information");
info.SetAttribute("姓名", "雷神所爾");
info.SetAttribute("電話", "8825251");
//加入至members節點底下
members.AppendChild(info);
doc.Save("Test111.xml");//存檔成xml

//輸出為字串變數
// Now create StringWriter object to get data from xml document.
StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
doc.WriteTo(xw);
String XmlString = sw.ToString();
Console.WriteLine(XmlString);
Console.WriteLine("press any key to continue!");
Console.ReadKey();o);
doc.Save("Test111.xml");//存檔成xml

//輸出為字串變數
// Now create StringWriter object to get data from xml document.
StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
doc.WriteTo(xw);
String XmlString = sw.ToString();
Console.WriteLine(XmlString);
Console.WriteLine("press any key to continue!");
Console.ReadKey();

產生出來長這樣:

<?xml version="1.0" encoding="UTF-8"?>
<?MyCustomNameHere attribute1="val1" attribute2="val2"?>
<foo:bar xmlns:foo="http://tempuri.org/foo">
  <Company>
    <Department 部門="程式部" 部門經理="鹽巴">
      <Members>
        <Information 姓名="鹽巴" 電話="3939889" />
        <Information 姓名="雷神所爾" 電話="8825251" />
      </Members>
    </Department>
  </Company>
</foo:bar>




參考文章:
How to convert XML to String
https://forums.asp.net/t/1641800.aspx?How+to+convert+XML+to+String
[C#.NET][XML] XML檔案基本操作-XmlDocument
https://dotblogs.com.tw/yc421206/archive/2010/08/10/17108.aspx