1

単純なxmlfileがあり、それはツリービューに配置されます

 <bookstore xmlns="generic">
     <book genre="Book" >`
       <title>Book of Benjamin Franklin</title>
       <author>
         <first-name>Benson</first-name>
         <last-name>Frank</last-name>
      </author>
      <price>89.88</price> 
     </book>
     <book genre="autobiography">
        <title>The Autobiography of Benjamin Franklin</title> 
        <author>
           <first-name>Ben</first-name> 
           <middlename /> 
           <last-name>Franklin</last-name> 
        </author>
        <price>89.88</price> 
     </book>
   <book genre="novel" >
     <title>The Confidence Man</title> 
     <author>
       <first-name>John</first-name> 
       <last-name>Melville</last-name> 
     </author>
    <price>11.99</price> 
   </book>
</bookstore>

交換したい(2要素目)

<book genre="autobiography" >
    <title>The Autobiography of Benjamin Franklin</title> 
    <author>
       <first-name>Ben</first-name> 
       <middlename /> 
       <last-name>Franklin</last-name> 
    </author>
    <price>89.88</price> 
 </book>

別の要素で(編集され、部分を文字列として編集しました)

<book genre="autobiography">
   <title>The Autobiography of Benjamin Franklin</title>
   <author>
     <first-name>Ben</first-name>
     <last-name>Franklin</last-name>
   </author>
   <price>89.88</price>
</book>

私が使用したコードはここにあります

            XElement XmlTree = XElement.Parse(text);//text contain edited portion
            XmlElement XMLE = (XmlElement)TreeV.SelectedItem;


            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(scd_file);

            XmlDocumentFragment xfrag = xdoc.CreateDocumentFragment();
            xfrag.InnerXml = text;
            XmlDocumentFragment xfrag1 = xdoc.CreateDocumentFragment();
            xfrag1.InnerXml = XMLE.OuterXml;

            xdoc.ReplaceChild(xfrag, xfrag1);

しかし、エラーが表示されます(xfrag1はxdocのノードではありません)。この問題を解決するのを手伝ってください。

4

1 に答える 1

0

私が解決策を得たすべてに感謝します。

  XmlElement XMLE = (XmlElement)TreeV.SelectedItem;// selection from treeview(TreeV) that we want to edit (2nd element)
  XmlDocument XD = new XmlDocument();
  XD.LoadXml(text);// text is edited part save as a string
  XmlNode root=XD.DocumentElement;
  XmlDocument xml = XMLE.ParentNode.OwnerDocument;
  XmlNode import= xml.ImportNode(root, true);
  XMLE.ParentNode.ReplaceChild(import, XMLE);
  xml.Save(scd_file); //scd_file path
于 2012-09-27T09:39:39.237 に答える