writing to xml file

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

I am a newbie to XML. I am trying to replace a node by using the ReplaceChild
function. The element s in the node to be replaced all have a prefix of"mml".
However when I try using the following code

XmlElement elem = doc.CreateElement("mml","cn",xmlns.ToString());
elem.InnerText = "mytext";
mathExprList[0].ReplaceChild(elem, mathExprList[0].LastChild);
doc.Save(@"..\..\MathML1.xml");

I get the following line in my XML document:
<mml:cn xmlns:mml="System.Xml.XmlNamespaceManager">mytext</mml:cn>

I would like to get this instead:
<mml:cn>mytext</mml:cn>
 
I don't have my C# tools handy to test, but isn't that just:
XmlElement el = doc.CreateElement("mml:cn");
etc

Marc
 
Dave said:
I am a newbie to XML. I am trying to replace a node by using the ReplaceChild
function. The element s in the node to be replaced all have a prefix of"mml".
However when I try using the following code

XmlElement elem = doc.CreateElement("mml","cn",xmlns.ToString());
elem.InnerText = "mytext";
mathExprList[0].ReplaceChild(elem, mathExprList[0].LastChild);
doc.Save(@"..\..\MathML1.xml");

I get the following line in my XML document:
<mml:cn xmlns:mml="System.Xml.XmlNamespaceManager">mytext</mml:cn>

I would like to get this instead:
<mml:cn>mytext</mml:cn>

The prefix 'mml' needs to be bound to a namespace URI. Which namespace
do you want the element to be in? Perhaps
XmlElement elem = doc.CreateElement("mml", "cn",
mathExprList[0].GetNamespaceOfPrefix("mml"));
does what you want.
If not, show us your XML.
 
Back
Top