2011-01-13 16 views
15

Ho un xml come:Come scrivere DatiC in xml

<?xml version="1.0" encoding="UTF-8"?> 
<entry> 
    <entry_id></entry_id> 
    <entry_status></entry_status> 
    </entry> 

sto scrivendo i dati in esso, come:

XmlNode xnode = xdoc.SelectSingleNode("entry/entry_status"); 
xnode.InnerText = "<![CDATA[ " + Convert.ToString(sqlReader["story_status"]) + " ]]>" ;  

ma il suo cambiamento "<" a "& lt" di CDATA . Ti prego, dimmi come riempire i valori sopra xml come un formato CData.

so che possiamo creare CDATA come:

XmlNode itemDescription = doc.CreateElement("description"); 
XmlCDataSection cdata = doc.CreateCDataSection("<P>hello world</P>"); 
itemDescription.AppendChild(cdata); 
item.AppendChild(itemDescription); 

ma il mio processo è quello di leggere il nodo di XML e cambiare il suo valore non è da aggiungere in esso. Grazie

risposta

14

Ti davvero bisogno di essere in CDATA, o volete solo per ottenere il testo in là in un modo che non richiederà più fuggire nel tuo codice?

InnerText esegua qualsiasi fuga è richiesto, per cui in genere mi basta usare

xnode.InnerText = Convert.ToString(sqlReader["story_status"]); 

... ma se davvero desidera un CDATA node, è possibile creare uno voi stessi come per la risposta di Nekresh.

+0

Grande quello che significa il modo in cui sto eseguendo la scrittura in xml. il personaggio di escape verrà gestito automaticamente e il lettore xml non si troverà ad affrontare alcun problema e il mio xml non si romperà ... ok? –

+0

@Rajesh: esattamente. Puoi dire che l'impostazione della proprietà 'InnerText' esegue l'escaping perché è esattamente il motivo per cui il tuo approccio attuale non funziona: sta sfuggendo al testo"

16

come descritto di seguito: msdn

// Create an XmlCDataSection from your document 
var cdata = xdoc.CreateCDataSection(Convert.ToString(sqlReader["story_status"])); 

// Append the cdata section to your node 
xnode.AppendChild(cdata); 
+0

Non voglio aggiungere. Voglio cambiare il suo valore .. gli elementi sono già lì –

0

È possibile utilizzare writer.WriteCData(value);

dove lo scrittore è XmlWriter oggetto.

1

Utilizzare Node.InnerXml, non Node.InnerText. Node.InnerText sostituisce automaticamente i valori speciali. Notare che se si specifica con CDATA in InnerXml, Node.InnerText è testo in CDATA. Esempio:

public class Test 
{ 
    public static int Main(string[] args) 
    { 
     const string xmlTxt = @"<?xml version=""1.0"" encoding=""UTF-8""?> 
<entry> 
    <entry_id></entry_id> 
    <entry_status></entry_status> 
    </entry>"; 
     TextReader treader = new StringReader(xmlTxt); 
     XmlReader xreader = XmlReader.Create(treader); 
     XmlDocument xdoc = new XmlDocument(); 
     xdoc.Load(xreader); 

     XmlNode xnode = xdoc.SelectSingleNode("entry/entry_status"); 
     //xnode.InnerText = "<![CDATA[something]]>"; 
     xnode.InnerXml = "<![CDATA[something]]>"; 
     Console.WriteLine("inner text is: " + xnode.InnerText); 

     xdoc.Save(Console.Out); Console.WriteLine(); 

     return 0; 
    } 
} 

uscita del Programma:

 
inner text is: something 
<?xml version="1.0" encoding="ibm852"?> 
<entry> 
    <entry_id> 
    </entry_id> 
    <entry_status><![CDATA[something]]></entry_status> 
</entry> 
10

Se davvero bisogno di una sezione CDATA (vedi Jon's answer), è possibile ottenere che in questo modo:

XmlNode xnode = xdoc.SelectSingleNode("entry/entry_status"); 
XmlCDataSection cdata = xdoc.CreateCDataSection(Convert.ToString(sqlReader["story_status"])); 
xnode.InnerXml = cdata.OuterXml; 

Questo sarà sostituire il contenuto di xnode, non accluderlo.

2
XmlNode childNode = node.ChildNodes[0]; 
if (childNode is XmlCDataSection) 
{ 
    XmlCDataSection cdataSection = childNode as XmlCDataSection; 
    cdataSection.Value = newValue; 
}