1

これが私がする必要があることです:サンプルXML(それがここに表示されるかどうかはわかりません)

 <Tags>
 <Tag ID="0" UserTotal="1" AllowMultipleSelect="1">
  <Name>BaseSamples</Name> 
  <Sample ID="546" Count="1">Sample1 </Sample> 
  <Sample ID="135" Count="1">Sample99</Sample> 
  <Sample ID="544" Count="1">Sample2</Sample> 
  <Sample ID="5818" Count="1">Sample45</Sample> 
  </Tag>
  </Tags>

削除したい:

<Sample ID="135" Count="1">Sample99</Sample>

XMLを次のように返します。

 <Tags>
 <Tag ID="0" UserTotal="1" AllowMultipleSelect="1">
  <Name>BaseSamples</Name> 
  <Sample ID="546" Count="1">Sample1 </Sample>   
  <Sample ID="544" Count="1">Sample2</Sample> 
  <Sample ID="5818" Count="1">Sample45</Sample> 
  </Tag>
  </Tags>

ヘルプ/ヒントをいただければ幸いです。着信サンプルの「ID」属性と「SampleName」(要素のCDATA)がわかります。

4

3 に答える 3

2

あなたはC#でこのようなことをすることができるはずです

 XmlDocument xmlDoc = new XmlDocument();
 xmlDoc.Load("XMLFile.xml");     
 XmlNode node = xmlDoc.SelectSingleNode("/Tags/Tag/Sample[@ID='135']");
 XmlNode parentNode = node.ParentNode;
 if (node != null) {
   parentNode.RemoveChild(node);
 }
 xmlDoc.Save("NewXMLFileName.xml");
于 2010-07-16T01:21:10.670 に答える
1

XMLに対してこのスタイルシートを実行すると、目的の出力が生成されます。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" />

    <!--identity template copies all content forward -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <!--empty template will prevent this element from being copied forward-->
    <xsl:template match="Sample[@ID='135']"/>

</xsl:stylesheet>
于 2010-07-15T01:10:45.903 に答える
1

マッズ・ハンセンの答えをありがとう、それはとても役に立ちました!!! 他のすべてにも感謝します!はい、私の道は間違っていました。コードは機能しますが、私の場合、「保存」を実行するとエラーが発生していました。情報を保存するために同じ文字列を使用していました(回答例で述べたように、newfile.xmlではありません)。おそらくそれが私に問題を引き起こしていました、これがこの新しい問題を解決するために私がしたことです:

 XmlDocument workingDocument = new XmlDocument();  
 workingDocument.LoadXml(sampleInfo); //sampleInfo comes in as a string.
 int SampleID = SampleID;  //the SampleID comes in as an int.   

 XmlNode currentNode;
 XmlNode parentNode;  
 // workingDocument.RemoveChild(workingDocument.DocumentElement.SelectSingleNode("/Tags/Tag/Sample[@ID=SampleID]"));
  if (workingDocument.DocumentElement.HasChildNodes)
   {                              
                           //This won't work:   currentNode = workingDocument.RemoveChild(workingDocument.SelectSingleNode("//Sample[@ID=" + SampleID + "]"));
                          currentNode = workingDocument.SelectSingleNode("Tags/Tag/Sample[@ID=" + SampleID + "]");
                          parentNode = currentNode.ParentNode;
                          if (currentNode != null)
                          {
                              parentNode.RemoveChild(currentNode);
                          }                              


                // workingDocument.Save(sampleInfo);

                           sampleInfo = workingDocument.InnerXml.ToString();
}
于 2010-07-16T17:12:23.773 に答える