469,271 Members | 1,431 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

How do you remove and insert xml in C#

grs
All,

In the xml document below I want to delete the <eConnectProcessInfo
xsi:nil="true" />
and insert
<eConnectProcessInfo>
<ConnectionString>data source=SERVERName ...etc</ConnectionString>
</eConnectProcessInfo?

Will someone please give me some C# code to accomplish the task.

thanks
grs
==========
<?xml version="1.0"?>
<eConnect xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<GLTransactionType>
<eConnectProcessInfo xsi:nil="true" />
<taGLTransactionLineInsert_Items>
<taGLTransactionLineInsert>
<BACHNUMB>BATCHNBR123</BACHNUMB>
<JRNENTRY>1000001</JRNENTRY>
<CRDTAMNT>2.50</CRDTAMNT>
<DEBITAMT>0</DEBITAMT>
<ACTNUMST>00-0000-10700-0000-0000</ACTNUMST>
</taGLTransactionLineInsert>
<taGLTransactionLineInsert>
<BACHNUMB>BATCHNBR123</BACHNUMB>
<JRNENTRY>1000001</JRNENTRY>
<CRDTAMNT>0</CRDTAMNT>
<DEBITAMT>2.50</DEBITAMT>
<ACTNUMST>00-1000-60100-0000-EBAY</ACTNUMST>
</taGLTransactionLineInsert>
</taGLTransactionLineInsert_Items>
<taGLTransactionHeaderInsert>
<BACHNUMB>BATCHNBR123</BACHNUMB>
<JRNENTRY>1000001</JRNENTRY>
<REFRENCE>AAA000000009</REFRENCE>
<TRXDATE>2005-05-10</TRXDATE>
<TRXTYPE>0</TRXTYPE>
</taGLTransactionHeaderInsert>
<taMdaUpdate_Items xsi:nil="true" />
</GLTransactionType>
</eConnect>
Nov 15 '05 #1
4 10184
Hi grs,

The easiest way is using DOM. Assumming that you have loaded that xml
document (using XmlDocument.Load, or XmlDocument.LoadXml).
In the xml document below I want to delete XmlElement root = xmlDoc.DocumentElement;
root.RemoveChild(root.SelectSingleNode("eConnectPr ocessInfo");
and insert

XmlElement node1 = xmlDoc.CreateElement("eConnectProcessInfo");
XmlElement node2 = xmlDoc.CreateElement("ConnectionString");
node2.InnerText = "data source=SERVERName ...etc";
node1.AppendChild(node2);
root.AppendChild(node1);

You can also use root.ReplaceChild for your purpose. You can also
remove the "xsi:nil" attribute and append the "ConnectionString"
element only.

Further info could be found in .NET documentation.

Thi.

Nov 16 '05 #2
Hi grs,

You can load the whole document into an XmlDocument object and add/remove
nodes in it. You can try to following code snippet to achieve this. I
tested it on my machine and it works fine.

XmlDocument doc = new XmlDocument();
doc.Load(@"e:\test1.xml");
XmlNode n =
doc.SelectSingleNode("//eConnect/GLTransactionType/eConnectProcessInfo");
n.RemoveAll();
XmlElement newElem = doc.CreateElement("ConnectionString");
newElem.InnerText = "data source=SERVERName ...etc";
n.AppendChild(newElem);

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 16 '05 #3
grs
Truong and Kevin,
Thank you !
grs

"grs" <gs****@budgetext.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
All,

In the xml document below I want to delete the <eConnectProcessInfo
xsi:nil="true" />
and insert
<eConnectProcessInfo>
<ConnectionString>data source=SERVERName ...etc</ConnectionString>
</eConnectProcessInfo?

Will someone please give me some C# code to accomplish the task.

thanks
grs
==========
<?xml version="1.0"?>
<eConnect xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<GLTransactionType>
<eConnectProcessInfo xsi:nil="true" />
<taGLTransactionLineInsert_Items>
<taGLTransactionLineInsert>
<BACHNUMB>BATCHNBR123</BACHNUMB>
<JRNENTRY>1000001</JRNENTRY>
<CRDTAMNT>2.50</CRDTAMNT>
<DEBITAMT>0</DEBITAMT>
<ACTNUMST>00-0000-10700-0000-0000</ACTNUMST>
</taGLTransactionLineInsert>
<taGLTransactionLineInsert>
<BACHNUMB>BATCHNBR123</BACHNUMB>
<JRNENTRY>1000001</JRNENTRY>
<CRDTAMNT>0</CRDTAMNT>
<DEBITAMT>2.50</DEBITAMT>
<ACTNUMST>00-1000-60100-0000-EBAY</ACTNUMST>
</taGLTransactionLineInsert>
</taGLTransactionLineInsert_Items>
<taGLTransactionHeaderInsert>
<BACHNUMB>BATCHNBR123</BACHNUMB>
<JRNENTRY>1000001</JRNENTRY>
<REFRENCE>AAA000000009</REFRENCE>
<TRXDATE>2005-05-10</TRXDATE>
<TRXTYPE>0</TRXTYPE>
</taGLTransactionHeaderInsert>
<taMdaUpdate_Items xsi:nil="true" />
</GLTransactionType>
</eConnect>

Nov 16 '05 #4
You're welcome.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Diego Rey | last post: by
6 posts views Thread by Arne Claus | last post: by
7 posts views Thread by sanjana | last post: by
1 post views Thread by Glenn | last post: by
3 posts views Thread by Markus | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.