470,815 Members | 2,831 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

insert xml element in a speicific location

let's say i have a xml file

<root>
<stuff id='1'>
<stuff1>
</stuff1>
</stuff>
</root>

how do i insert new <stuff2>element and atrribute after stuff1
Jun 26 '06 #1
3 5228


dotnetnoob wrote:
let's say i have a xml file

<root>
<stuff id='1'>
<stuff1>
</stuff1>
</stuff>
</root>

how do i insert new <stuff2>element and atrribute after stuff1


You can do that with the DOM e.g. (C#)
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"file.xml");
xmlElement stuff =
xmlDocument.SelectSingleNode("root/stuff[@id = '1']") as XmlElement;
if (xmlElement != null) {
XmlElement stuff2 = xmlDocument.CreateElement("stuff2");
stuff.AppendChild(stuff2);
}
xmlDocument.Save(@"file.xml");
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 26 '06 #2
hi, thank you for your help but it didn't added to where i want it to be here
is a fragment of xml file that i'm working on

<EventEnrollment InstanceNumber="3747">
<BackwardReferences/>
<EventEnrollmentProperties AckRequired="0" EventEnable="0" EventType="0"
NotifyClass="0" NotifyType="0" PresentValue="0" ToFaultText=""
ToNormalText="" ToOffNormalText="">
<EnabledReference ObjectKey="" PropertyName=""/>
<EventParameters BitMask="0" Deadband="" HighDiffLimit="" HighLimit=""
ListOfBitStringValues="" ListOfValues="" LowDiffLimit="" LowLimit=""
ReferencedPropertyIncrement="1" TimeDelay="60" TimeInterval="1">
<SetpointReference ObjectKey="" PropertyName=""/>
</EventParameters>
<EventSources/>
</EventEnrollmentProperties>
<GeneralProperties Attr="0" InstanceNumber="3747" IsConfigured="0"
ObjectName="test" ObjectType="9" SchemaVer="1.4" TraceOptions="0">
<Description/>
</GeneralProperties>
<EventEnrollment InstanceNumber="9000"/>
</EventEnrollment>
i'll like to create a new EventEnrollment block right after the last
EventEnrollment. how did it got appened into the last EventEnrollment and not
by itself.

"Martin Honnen" wrote:


dotnetnoob wrote:
let's say i have a xml file

<root>
<stuff id='1'>
<stuff1>
</stuff1>
</stuff>
</root>

how do i insert new <stuff2>element and atrribute after stuff1


You can do that with the DOM e.g. (C#)
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"file.xml");
xmlElement stuff =
xmlDocument.SelectSingleNode("root/stuff[@id = '1']") as XmlElement;
if (xmlElement != null) {
XmlElement stuff2 = xmlDocument.CreateElement("stuff2");
stuff.AppendChild(stuff2);
}
xmlDocument.Save(@"file.xml");
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Jun 26 '06 #3
nevermind, i got it

thank

"dotnetnoob" wrote:
let's say i have a xml file

<root>
<stuff id='1'>
<stuff1>
</stuff1>
</stuff>
</root>

how do i insert new <stuff2>element and atrribute after stuff1

Jun 26 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Abareblue | last post: by
5 posts views Thread by Andrew | last post: by
4 posts views Thread by Nick Keighley | last post: by
5 posts views Thread by asdf | last post: by
2 posts views Thread by mirandacascade | last post: by
4 posts views Thread by Tim Slattery | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.