2

这是我想要得到的xml文件。它包含第 01,02,03 号及以后的灾难详细信息。灾难由灾难 ID 标识。

<?xml version="1.0" encoding="utf-8" ?>
<disasters>
  <disaster ID="001"
      disaterType="Flood"
      location="Matara"
      date="20-01-2012"
      noOfVictims="245">
      <victims>
          <victim type="adult"
               gender="male"
               amount="46"
               />
        <victim type="children"
             gender="female"
             amount="460"/>
      </victims>
          <requiredItems>
             <requiredItem type="Food"
                  itemName="Rice" 
                  description="null"
                  quantity="367"/>
             <requiredItem type="Stationary"
                  itemName="Pens"
                  description="null"
                  quantity="87"/>
          </requiredItems>
  </disaster>
  <disaster ID="002" // This is a separate disaster detail list
     disaterType="Tsunami"
     location="Galle"
     date="10-05-2009"
     noOfVictims="845">
      <victims>
          <victim type="children"
               gender="male"
               amount="46"
               />
          <victim type="children"
               gender="female"
               amount="460"/>
      </victims>
         <requiredItems>
           <requiredItem type="Clothes"
                itemName="Tshirts"
                description="male"
                quantity="67"/>
           <requiredItem type="Food"
                itemName="bread"
                description="null"
                quantity="37"/>
         </requiredItems>  
  </disaster> // end of the second list of disaster details
</disasters>

这是我的代码。但它只会添加新的灾难 ID。其余详细信息附加错误。** 代码将附加到第一个灾难 ID,但我想将它们添加到第二个灾难并将它们标识为新灾难。提前致谢

        if (System.IO.File.Exists(path))
        {
        XmlDocument xDoc = new XmlDocument();
            xDoc.Load("C:\\4 ITP la laaa\\me doing\\disasters.xml");
            XmlNode xNode = xDoc.CreateNode(XmlNodeType.Element, "disaster", "");
            XmlAttribute id = xDoc.CreateAttribute("ID");
            XmlAttribute disaterType = xDoc.CreateAttribute("value");
            XmlAttribute location = xDoc.CreateAttribute("value1");
            XmlAttribute date = xDoc.CreateAttribute("value2");
            XmlAttribute noOfVictims = xDoc.CreateAttribute("value3");
            id.Value = "001";
            disaterType.Value = "Flood";
            date.Value = " value1";
            noOfVictims.Value = "date";
            xNode.Attributes.Append(id);
            xNode.Attributes.Append(disaterType);
            --
            --

            XmlNode xNode1 = xDoc.CreateNode(XmlNodeType.Element, "victims", "");
            XmlAttribute type = xDoc.CreateAttribute("type");

            type.Value = "adult";
            gender.Value = "male";
            value.Value = "46";
            --
            --
            xDoc.GetElementsByTagName("disasters")[0].InsertAfter(xNode,        xDoc.GetElementsByTagName("disasters")[0].LastChild);
            **xDoc.GetElementsByTagName("victims")[0].InsertAfter(xNode1, xDoc.GetElementsByTagName("victims")[0].LastChild);**

            xDoc.Save("C:\\4 ITP la laaa\\me doing\\disasters.xml");
            Label1.Text=("Apended");
4

1 回答 1

0

您的代码似乎太复杂了。我只修了**一个。

xDoc.GetElementsByTagName("disasters")[0].InsertAfter(xNode, 
    xDoc.GetElementsByTagName("disasters")[0].LastChild);

//xDoc.GetElementsByTagName("victims")[0].InsertAfter(xNode1, 
//       xDoc.GetElementsByTagName("victims")[0].LastChild);
xNode.AppendChild(xNode1);  // <visitors> is simply a direct child of <disaster>

首选方式using System.Xml.Linq;

XDocument xDoc = XDocument.Load("ExistingFile.xml");

XElement newDisaster = new XElement("disaster", 
        new XAttribute("ID", 2),
        new XAttribute("location", "Galle"),
        ...
        new XElement("victims", 
           new XElement("victim", 
              new XAttribute("type", "children"),
              ...
       )));

xDoc.Element("disasters").Add(newDisaster);
xDoc.Save("SomeFileName.xml");
于 2013-09-21T11:07:09.503 回答