所以,我试图找出如何为根元素创建子元素。
我正在创建一个应用程序,用于计算电脑游戏中城市之间最便宜的旅行费用。
在游戏中,大约有13个城市可以在这两个城市之间旅行,但需要付费。
现在费用总是一样的,但因城市而异。在某些情况下,通过另一个城市旅行而不是单程直达目的地会更便宜。有时两个城市之间根本没有联系,迫使你走另一条路。
让我们说我们在 CITY A ,并希望转到 CITY B 。 如果 A 与 B 之间没有直接联系,我们必须采取不同的路线。
CITY C 与 A 和 B 都有关联,所以这是一个选项,但如果 CITY D 会怎样还可以连接到 A 和 B 但价格更便宜?
我希望你能看到我在哪里。现在让我们来创建XML文档。
private void exportXMLfileToolStripMenuItem_Click(object sender, EventArgs e)
{
//XmlDocument doc = new XmlDocument();
#region text
XmlTextWriter writer = new XmlTextWriter("city.xml", System.Text.Encoding.UTF8);
writer.WriteStartDocument(true);
writer.Formatting = Formatting.Indented;
writer.Indentation = 2;
writer.WriteStartElement("Table");
createNode("CITY A", writer); //THE NAME OF A CITY
createElement("CITY C", 120, writer); // "CITY A" HAS CONNECTIONS TO THEESE CITIES. (ELEMENTS)
createElement("CITY D", 60, writer);
createNode("CITY B", writer); //CITY NAME
createElement("CITY C", 130, writer); //CITY CONNECTIONS
createElement("CITY D", 70, writer);
createNode("CITY C", writer); //CITY NAME
createElement("CITY A", 120, writer); //CITY CONNECTIONS
createElement("CITY B", 130, writer);
createNode("CITY D", writer);//CITY NAME
createElement("CITY A", 60, writer); //CITY CONNECTIONS
createElement("CITY B", 70, writer);
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
#endregion
}
private void createNode(string mainCity, XmlTextWriter writer)
{
writer.WriteStartElement("BaseCity");
writer.WriteStartAttribute("Name");
writer.WriteString(mainCity);
writer.WriteEndAttribute();
writer.WriteEndElement();
}
private void createElement(string cityName, int price, XmlTextWriter writer)
{
writer.WriteStartElement("City");
writer.WriteStartAttribute("Name");
writer.WriteString(cityName);
writer.WriteStartElement("Price");
writer.WriteString(price.ToString());
writer.WriteEndElement();
writer.WriteEndElement();
}
}
这是输出: 没有结构......
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
-<Table>
<BaseCity Name="CITY A"/>
-<City Name="CITY C">
<Price>120</Price>
</City>
-<City Name="CITY D">
<Price>60</Price>
</City>
<BaseCity Name="CITY B"/>
-<City Name="CITY C">
<Price>130</Price>
</City>
-<City Name="CITY D">
<Price>70</Price>
</City>
<BaseCity Name="CITY C"/>
-<City Name="CITY A">
<Price>120</Price>
</City>
-<City Name="CITY B">
<Price>130</Price>
</City>
<BaseCity Name="CITY D"/>
-<City Name="CITY A">
<Price>60</Price>
</City>
-<City Name="CITY B">
<Price>70</Price>
</City>
</Table>
我希望它是这样的:
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
-<Table>
<BaseCity Name="CITY A"/>
-<City Name="CITY C">
<Price>120</Price>
</City>
-<City Name="CITY D">
<Price>60</Price>
</City>
<BaseCity Name="CITY B"/>
-<City Name="CITY C">
<Price>130</Price>
</City>
-<City Name="CITY D">
<Price>70</Price>
</City>
答案 0 :(得分:0)
使用LINQ to XML解决:
XDocument doc = new XDocument(
new XElement("Cities",
new XElement("AbDendriel",
new XElement("Carlin", "80"),
new XElement("Edron", "70"),
new XElement("Thais", "130"),
new XElement("Venore", "90"),
new XElement("Yalahar", "160")),
new XElement("Ankrahmun",
new XElement("Darashia", "100"),
new XElement("Edron", "160"),
new XElement("LibertyBay", "90"),
new XElement("PortHope", "80"),
new XElement("Venore", "90"),
new XElement("Yalahar", "160")),
new XElement("Carlin",
new XElement("AbDendriel", "80"),
new XElement("Edron", "110"),
new XElement("Svargrond", "110"),
new XElement("Thais", "110"),
new XElement("Venore", "130"),
new XElement("Yalahar", "160")),
new XElement("Darashia",
new XElement("Ankrahmun", "100"),
new XElement("LibertyBay", "200"),
new XElement("PortHope", "180"),
new XElement("Thais", "160"),
new XElement("Venore", "60"),
new XElement("Yalahar", "210")),
new XElement("Edron",
new XElement("AbDendriel", "70"),
new XElement("Ankrahmun", "160"),
new XElement("Carlin", "110"),
new XElement("LibertyBay", "170"),
new XElement("PortHope", "150"),
new XElement("Thais", "160"),
new XElement("Venore", "40")),
new XElement("LibertyBay",
new XElement("Ankrahmun", "90"),
new XElement("Darashia", "200"),
new XElement("Edron", "170"),
new XElement("PortHope", "50"),
new XElement("Thais", "180"),
new XElement("Venore", "180"),
new XElement("Yalahar", "275")),
new XElement("PortHope",
new XElement("Ankrahmun", "110"),
new XElement("Darashia", "180"),
new XElement("Edron", "150"),
new XElement("LibertyBay", "50"),
new XElement("Thais", "160"),
new XElement("Venore", "160"),
new XElement("Yalahar", "260")),
new XElement("Roshamuul",
new XElement("Thais", "210")),
new XElement("Oramond",
new XElement("Edron", "110"),
new XElement("PortHope", "200"),
new XElement("Thais", "150"),
new XElement("Venore", "130")),
new XElement("Svargrond",
new XElement("Carlin", "110"),
new XElement("Thais", "180"),
new XElement("Venore", "150")),
new XElement("Thais",
new XElement("AbDendriel", "130"),
new XElement("Carlin", "110"),
new XElement("Edron", "160"),
new XElement("LibertyBay", "180"),
new XElement("PortHope", "160"),
new XElement("Roshamuul", "210"),
new XElement("Oramond", "150"),
new XElement("Svargrond", "180"),
new XElement("Venore", "170"),
new XElement("Yalahar", "200")),
new XElement("Venore",
new XElement("AbDendriel", "90"),
new XElement("Ankrahmun", "150"),
new XElement("Carlin", "130"),
new XElement("Darashia", "60"),
new XElement("Edron", "40"),
new XElement("LibertyBay", "180"),
new XElement("PortHope", "160"),
new XElement("Svargrond", "150"),
new XElement("Thais", "170"),
new XElement("Yalahar", "185")),
new XElement("Yalahar",
new XElement("AbDendriel", "160"),
new XElement("Ankrahmun", "230"),
new XElement("Carlin", "185"),
new XElement("Darashia", "210"),
new XElement("LibertyBay", "275"),
new XElement("PortHope", "260"),
new XElement("Thais", "210"),
new XElement("Venore", "185"))));
doc.Save("city.xml");
输出:
<?xml version="1.0" encoding="UTF-8"?>
<Cities>
<AbDendriel>
<Carlin>80</Carlin>
<Edron>70</Edron>
<Thais>130</Thais>
<Venore>90</Venore>
<Yalahar>160</Yalahar>
</AbDendriel>
<Ankrahmun>
<Darashia>100</Darashia>
<Edron>160</Edron>
<LibertyBay>90</LibertyBay>
<PortHope>80</PortHope>
<Venore>90</Venore>
<Yalahar>160</Yalahar>
</Ankrahmun>
<Carlin>
<AbDendriel>80</AbDendriel>
<Edron>110</Edron>
<Svargrond>110</Svargrond>
<Thais>110</Thais>
<Venore>130</Venore>
<Yalahar>160</Yalahar>
</Carlin>
<Darashia>
<Ankrahmun>100</Ankrahmun>
<LibertyBay>200</LibertyBay>
<PortHope>180</PortHope>
<Thais>160</Thais>
<Venore>60</Venore>
<Yalahar>210</Yalahar>
</Darashia>
<Edron>
<AbDendriel>70</AbDendriel>
<Ankrahmun>160</Ankrahmun>
<Carlin>110</Carlin>
<LibertyBay>170</LibertyBay>
<PortHope>150</PortHope>
<Thais>160</Thais>
<Venore>40</Venore>
</Edron>
<LibertyBay>
<Ankrahmun>90</Ankrahmun>
<Darashia>200</Darashia>
<Edron>170</Edron>
<PortHope>50</PortHope>
<Thais>180</Thais>
<Venore>180</Venore>
<Yalahar>275</Yalahar>
</LibertyBay>
<PortHope>
<Ankrahmun>110</Ankrahmun>
<Darashia>180</Darashia>
<Edron>150</Edron>
<LibertyBay>50</LibertyBay>
<Thais>160</Thais>
<Venore>160</Venore>
<Yalahar>260</Yalahar>
</PortHope>
<Roshamuul>
<Thais>210</Thais>
</Roshamuul>
<Oramond>
<Edron>110</Edron>
<PortHope>200</PortHope>
<Thais>150</Thais>
<Venore>130</Venore>
</Oramond>
<Svargrond>
<Carlin>110</Carlin>
<Thais>180</Thais>
<Venore>150</Venore>
</Svargrond>
<Thais>
<AbDendriel>130</AbDendriel>
<Carlin>110</Carlin>
<Edron>160</Edron>
<LibertyBay>180</LibertyBay>
<PortHope>160</PortHope>
<Roshamuul>210</Roshamuul>
<Oramond>150</Oramond>
<Svargrond>180</Svargrond>
<Venore>170</Venore>
<Yalahar>200</Yalahar>
</Thais>
<Venore>
<AbDendriel>90</AbDendriel>
<Ankrahmun>150</Ankrahmun>
<Carlin>130</Carlin>
<Darashia>60</Darashia>
<Edron>40</Edron>
<LibertyBay>180</LibertyBay>
<PortHope>160</PortHope>
<Svargrond>150</Svargrond>
<Thais>170</Thais>
<Yalahar>185</Yalahar>
</Venore>
<Yalahar>
<AbDendriel>160</AbDendriel>
<Ankrahmun>230</Ankrahmun>
<Carlin>185</Carlin>
<Darashia>210</Darashia>
<LibertyBay>275</LibertyBay>
<PortHope>260</PortHope>
<Thais>210</Thais>
<Venore>185</Venore>
</Yalahar>
</Cities>