在xml的某个区域中添加元素

时间:2012-12-02 20:16:41

标签: c# xml winforms

之前我已回答过这个问题,但我将xml代码的形式改为更好一点。我是新手,但我有一个问题:以下内容是在运行时编写的。我可以添加所有需要但如果我想在PPE / ppe / UID“A3”之前添加元素我迷路了。以下是我所拥有的:

<?xml version="1.0" encoding="utf-8"?>
<Project>
  <Details>
    <details>
      ///....more specific data
    </details>
  </Details>
  <Tools />
  <Materials />
  <PPE>
    <ppe>
      <UID>A1</UID>
      <ppe1Bool>true</ppe1Bool>
      <ppe1>Test A1</ppe1>
      <ppe2Bool>true</ppe2Bool>
      <ppe2>....</ppe2>
    </ppe>
    <ppe>
      <UID>A3</UID>
      <ppe1Bool>true</ppe1Bool>
      <ppe1>Test A3</ppe1>
      <ppe2Bool>true</ppe2Bool>
      <ppe2>....</ppe2>
    </ppe>
  </PPE>
  <Video />
  <Links />
</Project>

这是我需要添加的,它需要在A3

之前
<?xml version="1.0" encoding="utf-8"?>
<Project>
  <Details>
    <details>
      ///....more specific data
    </details>
  </Details>
  <Tools />
  <Materials />
  <PPE>
    <ppe>
      <UID>A1</UID>
      <ppe1Bool>true</ppe1Bool>
      <ppe1>Test A1</ppe1>
      <ppe2Bool>true</ppe2Bool>
      <ppe2>....</ppe2>
    </ppe>
    //////////////////////////////
    <ppe>
      <UID>A2</UID>
      <ppe1Bool>true</ppe1Bool>
      <ppe1>New</ppe1>
      <ppe2Bool>true</ppe2Bool>
      <ppe2>....</ppe2>
    </ppe>
    /////////////////////////////
    <ppe>
      <UID>A3</UID>
      <ppe1Bool>true</ppe1Bool>
      <ppe1>Test A3</ppe1>
      <ppe2Bool>true</ppe2Bool>
      <ppe2>....</ppe2>
    </ppe>
  </PPE>
  <Video />
  <Links />
</Project>

这是我的代码,我很接近但不完全:

     public static void insertRBRowPPE(string strSelection, string strFileName)
     {
        System.Guid desiredGuid = System.Guid.NewGuid(); //I changed this line to A1..A2..A3 for
                                                         //clarity
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(strFileName);
        XmlNode node = xmlDoc.SelectSingleNode("Project/PPE/ppe");
        try
        {
            XElement doc = XElement.Load(strFileName);
            foreach (XElement elm in doc.Elements("PPE"))
            {
                if (node["UID"].InnerText == strSelection)
                {
                    elm.AddBeforeSelf(new XElement("ppe", new XElement("UID", desiredGuid),
                        new XElement("ppe1Bool", ""), new XElement("ppe1", "New"), new XElement("ppe2Bool", ""),
                        new XElement("ppe2", "")));
                }
            }
            doc.Save(strFileName);

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

所以..错误的结果是:

   ///xml code etc...
  <Materials />
  <ppe>////////////////////here but I dont want it here
    <UID>A2</UID>
    <ppe1Bool>true</ppe1Bool>
    <ppe1>New</ppe1>
    <ppe2Bool>true</ppe2Bool>
    <ppe2>....</ppe2>
  </ppe>//////////////////////////////
  <PPE>
    <ppe>
      <UID>A1</UID>
      <ppe1Bool>true</ppe1Bool>
      <ppe1>Test A1</ppe1>
      <ppe2Bool>true</ppe2Bool>
      <ppe2>....</ppe2>
    </ppe>
    //////////////////////////////
    I want it here....
    /////////////////////////////
    <ppe>
      <UID>A3</UID>
      <ppe1Bool>true</ppe1Bool>
      <ppe1>Test A3</ppe1>
      <ppe2Bool>true</ppe2Bool>
      <ppe2>....</ppe2>
    </ppe>
  </PPE>
  <Video />
  /// xml code etc....

对所有人才的帮助?谢谢!

2 个答案:

答案 0 :(得分:1)

根据上一个问题调整答案:

public static void insertRowBeforRowPPE(string strSelection, string strFileName)
{

  XElement doc=XElement.Load(strFileName);
  XElement project = doc.Element("Project");
  XElement ppe = project.Element("PPE");
  foreach(XElement elm in ppe.Elements("ppe")
  {
    if(elm.Element("UID").Value==strSelection)
      elm.AddBeforeSelf(new XElement("PPE",new XElement("UID","a2")));
      //adds PPE node having UID element with value a2 just before the required node
  }
  doc.Save(strFileName);
}

请注意,您可能希望添加更多检查 - 例如,如果XML不包含Project元素,则此代码会崩溃。

答案 1 :(得分:0)

MiMo,谢谢!一旦我看到它就很简单!我从你的更新中注释掉了一行,这很好。 再次感谢!

             //Those who may be interested
            //XElement project = doc.Element("Project");
            XElement ppe = doc.Element("PPE");
            foreach(XElement elm in ppe.Elements("ppe"))
            {
                if (elm.Element("UID").Value == strSelection)