0

私は次のようなxmlを持っています:-

<Comments>
    <Comment Text="ABC">
        <Note Timestamp="3/25/2013 8:26AM" Text="Movie">
    </Note>
</Comments>

コメントタグを同じ行で終了したいのですが、

<Comment Text="ABC"/>

私は文字列を適用しましたすべての操作を置き換えます:-

str.replaceAll("><Note", "/><Note");

ただし、コメントタグとNotesタグの間にスペースがないため、機能していません。スペースは毎回異なりますのでご注意ください。どうすればこれを達成できるか提案してください。

4

2 に答える 2

0

これがあなたのやり方による解決策です、

in = in.replaceAll(">[\\s]*<Note", "/><Note");

于 2013-03-27T09:50:01.590 に答える
0

Noteこれは、Linqを使用して、各ノードを新しいノードに置き換えることで実行できます。最初のノードのみを処理する簡略化された例(Linqpadでテスト済み):

 XElement oldStructure = 
     XElement.Parse(@"<Comment Text=""ABC"">
                       <Note Timestamp=""3/25/2013 8:26AM!"" Text=""Movie"">
                       </Note></Comment>");

 oldStructure.Dump("Original"); 

 // Replace this with some kind of lookup for each Note-element:
 var noteNode = (XElement) oldStructure.FirstNode; 

 // Create a new node. Note that it has no content:
 // (The null could be left out - left it here just to be explicit)
 var simplifiedNote = new XElement(noteNode.Name, null);
 noteNode.Attributes().ToList().ForEach(
    attrib => simplifiedNote.Add(new XAttribute(attrib.Name, attrib.Value)));

 // Replace with newly generated node - Linq will automatically use 
 // the compact format for you here, since the node has no content. 
 oldStructure.FirstNode.ReplaceWith(simplifiedNote);

 oldStructure.Dump("Final");

Linqpadでこれを実行すると、最初に以下がダンプされます。

オリジナル:

<Comment Text="ABC">
   <Note Timestamp="3/25/2013 8:26AM!" Text="Movie"></Note>
</Comment>

最後の:

<Comment Text="ABC">
   <Note Timestamp="3/25/2013 8:26AM!" Text="Movie" />
</Comment>
于 2013-03-27T10:59:19.537 に答える