inserting a node after a specific node in xml file Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How does one parse XML files?What's the difference between an element and a node in XML?How to create Excel (.XLS and .XLSX) file in C# without installing Ms Office?File Upload ASP.NET MVC 3.0Fastest Way of Inserting in Entity FrameworkFxCop Complaint: Exposed concrete xml types and a bad improvementwhat object can read mutiple nodesPreserve whitespace-only element content when deserializing XML using XmlSerializerC# Excel XML error when cloning rowPassing XMLDocument to Stored Procedure
How would a mousetrap for use in space work?
What to do with repeated rejections for phd position
Is CEO the "profession" with the most psychopaths?
A term for a woman complaining about things/begging in a cute/childish way
Crossing US/Canada Border for less than 24 hours
Do wooden building fires get hotter than 600°C?
How many time has Arya actually used Needle?
Why does it sometimes sound good to play a grace note as a lead in to a note in a melody?
Lagrange four-squares theorem --- deterministic complexity
What is an "asse" in Elizabethan English?
Is there public access to the Meteor Crater in Arizona?
Tannaka duality for semisimple groups
Amount of permutations on an NxNxN Rubik's Cube
Project Euler #1 in C++
How do I find out the mythology and history of my Fortress?
Is multiple magic items in one inherently imbalanced?
Why are vacuum tubes still used in amateur radios?
Drawing spherical mirrors
Electrolysis of water: Which equations to use? (IB Chem)
The Nth Gryphon Number
How can I prevent/balance waiting and turtling as a response to cooldown mechanics
Why does 14 CFR have skipped subparts in my ASA 2019 FAR/AIM book?
AppleTVs create a chatty alternate WiFi network
How much damage would a cupful of neutron star matter do to the Earth?
inserting a node after a specific node in xml file
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How does one parse XML files?What's the difference between an element and a node in XML?How to create Excel (.XLS and .XLSX) file in C# without installing Ms Office?File Upload ASP.NET MVC 3.0Fastest Way of Inserting in Entity FrameworkFxCop Complaint: Exposed concrete xml types and a bad improvementwhat object can read mutiple nodesPreserve whitespace-only element content when deserializing XML using XmlSerializerC# Excel XML error when cloning rowPassing XMLDocument to Stored Procedure
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
i'm re posting my question in a simpler way.
i need to search for a specific node in a XML file, and once i see it, i need to create a new node and insert it after that. the problem is that there are 2 nodes with the same value. and i need to insert the new node twice after each instance. with the code below: it's inserting the new nodes twice but in the same place after the first instance only.
original XML:
<eventlist>
<event type="AUDIOPLAYER">
<properties>
<schedule startType="-ParentEnd1" />
<media mediaType="Audio" />
</properties>
</event>
<event type="AUDIOPLAYER">
<properties>
<schedule startType="-ParentEnd2" />
<media mediaType="Audio" />
</properties>
</event>
</eventlist>
intended XML:
<eventlist>
<event type="AUDIOPLAYER">
<properties>
<schedule startType="-ParentEnd1" />
<media mediaType="Audio" />
</properties>
</event>
<event type="VIZ" />
<event type="AUDIOPLAYER">
<properties>
<schedule startType="-ParentEnd2" />
<media mediaType="Audio" />
</properties>
</event>
<event type="VIZ" />
</eventlist>
but the current output is:
<eventlist>
<event type="AUDIOPLAYER">
<properties>
<schedule startType="-ParentEnd1" />
<media mediaType="Audio" />
</properties>
</event>
<event type="VIZ" />
<event type="VIZ" />
<event type="AUDIOPLAYER">
<properties>
<schedule startType="-ParentEnd2" />
<media mediaType="Audio" />
</properties>
</event>
</eventlist>
the code is below here:
XmlDocument xdoc = new XmlDocument();
xdoc.Load(@"C:UsersnamokhtarDesktopnewxmltestxml.xml");
foreach (XmlNode node in xdoc.SelectNodes("/eventlist/event[@type='AUDIOPLAYER']"))
XmlNode srcNode = node.SelectSingleNode("/eventlist/event[@type='AUDIOPLAYER']");
XmlNode newElem = xdoc.CreateElement("event");
XmlAttribute newAttr = xdoc.CreateAttribute("type");
newAttr.Value = "VIZ";
newElem.Attributes.Append(newAttr);
srcNode.ParentNode.InsertAfter(newElem, srcNode);
xdoc.Save(@"C:UsersnamokhtarDesktopnewxmlnewxml1.xml");
please advise me...
c# xmlnode
add a comment |
i'm re posting my question in a simpler way.
i need to search for a specific node in a XML file, and once i see it, i need to create a new node and insert it after that. the problem is that there are 2 nodes with the same value. and i need to insert the new node twice after each instance. with the code below: it's inserting the new nodes twice but in the same place after the first instance only.
original XML:
<eventlist>
<event type="AUDIOPLAYER">
<properties>
<schedule startType="-ParentEnd1" />
<media mediaType="Audio" />
</properties>
</event>
<event type="AUDIOPLAYER">
<properties>
<schedule startType="-ParentEnd2" />
<media mediaType="Audio" />
</properties>
</event>
</eventlist>
intended XML:
<eventlist>
<event type="AUDIOPLAYER">
<properties>
<schedule startType="-ParentEnd1" />
<media mediaType="Audio" />
</properties>
</event>
<event type="VIZ" />
<event type="AUDIOPLAYER">
<properties>
<schedule startType="-ParentEnd2" />
<media mediaType="Audio" />
</properties>
</event>
<event type="VIZ" />
</eventlist>
but the current output is:
<eventlist>
<event type="AUDIOPLAYER">
<properties>
<schedule startType="-ParentEnd1" />
<media mediaType="Audio" />
</properties>
</event>
<event type="VIZ" />
<event type="VIZ" />
<event type="AUDIOPLAYER">
<properties>
<schedule startType="-ParentEnd2" />
<media mediaType="Audio" />
</properties>
</event>
</eventlist>
the code is below here:
XmlDocument xdoc = new XmlDocument();
xdoc.Load(@"C:UsersnamokhtarDesktopnewxmltestxml.xml");
foreach (XmlNode node in xdoc.SelectNodes("/eventlist/event[@type='AUDIOPLAYER']"))
XmlNode srcNode = node.SelectSingleNode("/eventlist/event[@type='AUDIOPLAYER']");
XmlNode newElem = xdoc.CreateElement("event");
XmlAttribute newAttr = xdoc.CreateAttribute("type");
newAttr.Value = "VIZ";
newElem.Attributes.Append(newAttr);
srcNode.ParentNode.InsertAfter(newElem, srcNode);
xdoc.Save(@"C:UsersnamokhtarDesktopnewxmlnewxml1.xml");
please advise me...
c# xmlnode
add a comment |
i'm re posting my question in a simpler way.
i need to search for a specific node in a XML file, and once i see it, i need to create a new node and insert it after that. the problem is that there are 2 nodes with the same value. and i need to insert the new node twice after each instance. with the code below: it's inserting the new nodes twice but in the same place after the first instance only.
original XML:
<eventlist>
<event type="AUDIOPLAYER">
<properties>
<schedule startType="-ParentEnd1" />
<media mediaType="Audio" />
</properties>
</event>
<event type="AUDIOPLAYER">
<properties>
<schedule startType="-ParentEnd2" />
<media mediaType="Audio" />
</properties>
</event>
</eventlist>
intended XML:
<eventlist>
<event type="AUDIOPLAYER">
<properties>
<schedule startType="-ParentEnd1" />
<media mediaType="Audio" />
</properties>
</event>
<event type="VIZ" />
<event type="AUDIOPLAYER">
<properties>
<schedule startType="-ParentEnd2" />
<media mediaType="Audio" />
</properties>
</event>
<event type="VIZ" />
</eventlist>
but the current output is:
<eventlist>
<event type="AUDIOPLAYER">
<properties>
<schedule startType="-ParentEnd1" />
<media mediaType="Audio" />
</properties>
</event>
<event type="VIZ" />
<event type="VIZ" />
<event type="AUDIOPLAYER">
<properties>
<schedule startType="-ParentEnd2" />
<media mediaType="Audio" />
</properties>
</event>
</eventlist>
the code is below here:
XmlDocument xdoc = new XmlDocument();
xdoc.Load(@"C:UsersnamokhtarDesktopnewxmltestxml.xml");
foreach (XmlNode node in xdoc.SelectNodes("/eventlist/event[@type='AUDIOPLAYER']"))
XmlNode srcNode = node.SelectSingleNode("/eventlist/event[@type='AUDIOPLAYER']");
XmlNode newElem = xdoc.CreateElement("event");
XmlAttribute newAttr = xdoc.CreateAttribute("type");
newAttr.Value = "VIZ";
newElem.Attributes.Append(newAttr);
srcNode.ParentNode.InsertAfter(newElem, srcNode);
xdoc.Save(@"C:UsersnamokhtarDesktopnewxmlnewxml1.xml");
please advise me...
c# xmlnode
i'm re posting my question in a simpler way.
i need to search for a specific node in a XML file, and once i see it, i need to create a new node and insert it after that. the problem is that there are 2 nodes with the same value. and i need to insert the new node twice after each instance. with the code below: it's inserting the new nodes twice but in the same place after the first instance only.
original XML:
<eventlist>
<event type="AUDIOPLAYER">
<properties>
<schedule startType="-ParentEnd1" />
<media mediaType="Audio" />
</properties>
</event>
<event type="AUDIOPLAYER">
<properties>
<schedule startType="-ParentEnd2" />
<media mediaType="Audio" />
</properties>
</event>
</eventlist>
intended XML:
<eventlist>
<event type="AUDIOPLAYER">
<properties>
<schedule startType="-ParentEnd1" />
<media mediaType="Audio" />
</properties>
</event>
<event type="VIZ" />
<event type="AUDIOPLAYER">
<properties>
<schedule startType="-ParentEnd2" />
<media mediaType="Audio" />
</properties>
</event>
<event type="VIZ" />
</eventlist>
but the current output is:
<eventlist>
<event type="AUDIOPLAYER">
<properties>
<schedule startType="-ParentEnd1" />
<media mediaType="Audio" />
</properties>
</event>
<event type="VIZ" />
<event type="VIZ" />
<event type="AUDIOPLAYER">
<properties>
<schedule startType="-ParentEnd2" />
<media mediaType="Audio" />
</properties>
</event>
</eventlist>
the code is below here:
XmlDocument xdoc = new XmlDocument();
xdoc.Load(@"C:UsersnamokhtarDesktopnewxmltestxml.xml");
foreach (XmlNode node in xdoc.SelectNodes("/eventlist/event[@type='AUDIOPLAYER']"))
XmlNode srcNode = node.SelectSingleNode("/eventlist/event[@type='AUDIOPLAYER']");
XmlNode newElem = xdoc.CreateElement("event");
XmlAttribute newAttr = xdoc.CreateAttribute("type");
newAttr.Value = "VIZ";
newElem.Attributes.Append(newAttr);
srcNode.ParentNode.InsertAfter(newElem, srcNode);
xdoc.Save(@"C:UsersnamokhtarDesktopnewxmlnewxml1.xml");
please advise me...
c# xmlnode
c# xmlnode
asked Mar 8 at 21:57
TitoTito
224
224
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
I haven´t fully tested this, but I´m almost sure this should do the trick:
foreach (XmlNode node in xdoc.SelectNodes("/eventlist/event[@type='AUDIOPLAYER']"))
XmlNodeList srcNodes = node.SelectNodes("/eventlist/event[@type='AUDIOPLAYER']");
foreach (XmlNode srcNode in srcNodes)
XmlNode newElem = xdoc.CreateElement("event");
XmlAttribute newAttr = xdoc.CreateAttribute("type");
newAttr.Value = "VIZ";
newElem.Attributes.Append(newAttr);
srcNode.ParentNode.InsertAfter(newElem, srcNode);
The problem is you were selecting single node from matching expression, and you need to select all the nodes that match that and insert the new node after each of them.
Hope this helps!
you are right, but i had to remove the first foreach loop. because it created the node twice. thanks again.
– Tito
Mar 8 at 22:29
add a comment |
Here is a solution using LINQ:
var xml = XDocument.Parse(File.ReadAllText(@"C:UsersnamokhtarDesktopnewxmltestxml.xml"));
var elems = xml.Root.Elements()
.Where(e => e.Name == "event" && e.Attribute("type")?.Value == "AUDIOPLAYER");
foreach (var elem in elems)
elem.AddAfterSelf(new XElement("event", new XAttribute("type", "VIZ")));
xml.Save(@"C:UsersnamokhtarDesktopnewxmlnewxml1.xml");
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55071557%2finserting-a-node-after-a-specific-node-in-xml-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I haven´t fully tested this, but I´m almost sure this should do the trick:
foreach (XmlNode node in xdoc.SelectNodes("/eventlist/event[@type='AUDIOPLAYER']"))
XmlNodeList srcNodes = node.SelectNodes("/eventlist/event[@type='AUDIOPLAYER']");
foreach (XmlNode srcNode in srcNodes)
XmlNode newElem = xdoc.CreateElement("event");
XmlAttribute newAttr = xdoc.CreateAttribute("type");
newAttr.Value = "VIZ";
newElem.Attributes.Append(newAttr);
srcNode.ParentNode.InsertAfter(newElem, srcNode);
The problem is you were selecting single node from matching expression, and you need to select all the nodes that match that and insert the new node after each of them.
Hope this helps!
you are right, but i had to remove the first foreach loop. because it created the node twice. thanks again.
– Tito
Mar 8 at 22:29
add a comment |
I haven´t fully tested this, but I´m almost sure this should do the trick:
foreach (XmlNode node in xdoc.SelectNodes("/eventlist/event[@type='AUDIOPLAYER']"))
XmlNodeList srcNodes = node.SelectNodes("/eventlist/event[@type='AUDIOPLAYER']");
foreach (XmlNode srcNode in srcNodes)
XmlNode newElem = xdoc.CreateElement("event");
XmlAttribute newAttr = xdoc.CreateAttribute("type");
newAttr.Value = "VIZ";
newElem.Attributes.Append(newAttr);
srcNode.ParentNode.InsertAfter(newElem, srcNode);
The problem is you were selecting single node from matching expression, and you need to select all the nodes that match that and insert the new node after each of them.
Hope this helps!
you are right, but i had to remove the first foreach loop. because it created the node twice. thanks again.
– Tito
Mar 8 at 22:29
add a comment |
I haven´t fully tested this, but I´m almost sure this should do the trick:
foreach (XmlNode node in xdoc.SelectNodes("/eventlist/event[@type='AUDIOPLAYER']"))
XmlNodeList srcNodes = node.SelectNodes("/eventlist/event[@type='AUDIOPLAYER']");
foreach (XmlNode srcNode in srcNodes)
XmlNode newElem = xdoc.CreateElement("event");
XmlAttribute newAttr = xdoc.CreateAttribute("type");
newAttr.Value = "VIZ";
newElem.Attributes.Append(newAttr);
srcNode.ParentNode.InsertAfter(newElem, srcNode);
The problem is you were selecting single node from matching expression, and you need to select all the nodes that match that and insert the new node after each of them.
Hope this helps!
I haven´t fully tested this, but I´m almost sure this should do the trick:
foreach (XmlNode node in xdoc.SelectNodes("/eventlist/event[@type='AUDIOPLAYER']"))
XmlNodeList srcNodes = node.SelectNodes("/eventlist/event[@type='AUDIOPLAYER']");
foreach (XmlNode srcNode in srcNodes)
XmlNode newElem = xdoc.CreateElement("event");
XmlAttribute newAttr = xdoc.CreateAttribute("type");
newAttr.Value = "VIZ";
newElem.Attributes.Append(newAttr);
srcNode.ParentNode.InsertAfter(newElem, srcNode);
The problem is you were selecting single node from matching expression, and you need to select all the nodes that match that and insert the new node after each of them.
Hope this helps!
answered Mar 8 at 22:14
MattMatt
21917
21917
you are right, but i had to remove the first foreach loop. because it created the node twice. thanks again.
– Tito
Mar 8 at 22:29
add a comment |
you are right, but i had to remove the first foreach loop. because it created the node twice. thanks again.
– Tito
Mar 8 at 22:29
you are right, but i had to remove the first foreach loop. because it created the node twice. thanks again.
– Tito
Mar 8 at 22:29
you are right, but i had to remove the first foreach loop. because it created the node twice. thanks again.
– Tito
Mar 8 at 22:29
add a comment |
Here is a solution using LINQ:
var xml = XDocument.Parse(File.ReadAllText(@"C:UsersnamokhtarDesktopnewxmltestxml.xml"));
var elems = xml.Root.Elements()
.Where(e => e.Name == "event" && e.Attribute("type")?.Value == "AUDIOPLAYER");
foreach (var elem in elems)
elem.AddAfterSelf(new XElement("event", new XAttribute("type", "VIZ")));
xml.Save(@"C:UsersnamokhtarDesktopnewxmlnewxml1.xml");
add a comment |
Here is a solution using LINQ:
var xml = XDocument.Parse(File.ReadAllText(@"C:UsersnamokhtarDesktopnewxmltestxml.xml"));
var elems = xml.Root.Elements()
.Where(e => e.Name == "event" && e.Attribute("type")?.Value == "AUDIOPLAYER");
foreach (var elem in elems)
elem.AddAfterSelf(new XElement("event", new XAttribute("type", "VIZ")));
xml.Save(@"C:UsersnamokhtarDesktopnewxmlnewxml1.xml");
add a comment |
Here is a solution using LINQ:
var xml = XDocument.Parse(File.ReadAllText(@"C:UsersnamokhtarDesktopnewxmltestxml.xml"));
var elems = xml.Root.Elements()
.Where(e => e.Name == "event" && e.Attribute("type")?.Value == "AUDIOPLAYER");
foreach (var elem in elems)
elem.AddAfterSelf(new XElement("event", new XAttribute("type", "VIZ")));
xml.Save(@"C:UsersnamokhtarDesktopnewxmlnewxml1.xml");
Here is a solution using LINQ:
var xml = XDocument.Parse(File.ReadAllText(@"C:UsersnamokhtarDesktopnewxmltestxml.xml"));
var elems = xml.Root.Elements()
.Where(e => e.Name == "event" && e.Attribute("type")?.Value == "AUDIOPLAYER");
foreach (var elem in elems)
elem.AddAfterSelf(new XElement("event", new XAttribute("type", "VIZ")));
xml.Save(@"C:UsersnamokhtarDesktopnewxmlnewxml1.xml");
answered Mar 8 at 22:29
jtatejtate
1,26321324
1,26321324
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55071557%2finserting-a-node-after-a-specific-node-in-xml-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown