473,382 Members | 1,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

embed xml

Hi,

I'm new to dotnet xml.

I want to embed my part of xml to existing xml data using xmldocument.
I think there is an option but i'm not getting exactly.

eg: DocumentElement.InsertAfter is used to insert the node but
reference is
problem ie how can i give ?

sample :

<span>
<div align="center">
< input id="myTxt" type="text"
style="background-color:LightSteelBlue;width:90%" autosuggest="true" />
<input type="button" value="myButton" onclick="myFunction();" />
</div>
</span>

from the above sample xml I want to insert a table tag before and after

input type=text tag.

Is possible ?
if yes pls suggest.

Thanks in advance.

Mar 3 '06 #1
8 1518
Hi Murali,

You can get the reference to the required "input" tag using a XPath
query. After that, just use the InsertBefore and InsertAfter functions
to insert away... !

The XPath to use here could be of the format :
".//span/div/input[@type='text']"
Try this :

==============================

Dim xDoc As New XmlDocument()
xDoc.Load("MyFile.xml")
Dim xList As XmlNode =
xDoc.SelectSingleNode(".//span/div/input[@type='text']")
Dim xNode, xParent As XmlNode
Dim xFrag As XmlDocumentFragment = xDoc.CreateDocumentFragment()
Dim xFrag2 As XmlDocumentFragment = xDoc.CreateDocumentFragment()
xFrag.InnerXml = "<TABLE id=""Table1"" cellspacing=""1""
cellpadding=""1"" width=""100%"" border=""0"" />"
xFrag2.InnerXml = "<TABLE id=""Table2"" cellspacing=""1""
cellpadding=""1"" width=""100%"" border=""0"" />"
xParent = xNode.ParentNode
xParent.InsertBefore(xFrag, xNode)
xParent.InsertAfter(xFrag2, xNode)
xDoc.Save("MyFile.xml"")

==============================

You did not give a lengthier part of your XML file, so I thought it
would be easier to explain using an XmlNode. The XPath expression
should match multiple "input" nodes, one for every "Span/div". In that
case, you can use the SelectNodes("") method to get an XmlNodeList and
iterate through that List.

HTH,

Regards,

Cerebrus.

Mar 3 '06 #2
Perhaps use this as an alternative solution :
================================================== =============

Dim xdoc As New XmlDocument
xdoc.LoadXml("<span><div align='center'><input id='myTxt'
type='text' style='background-color:LightSteelBlue;width:90%'
autosuggest='true'/><input type='button' value='myButton'
onclick='myFunction();' /> </div></span>")

Dim xmlOldEle As XmlElement =
xdoc.SelectSingleNode("//input[@type='text']")
Dim xmlNewEle As XmlElement = xdoc.CreateElement("table")
xmlNewEle.AppendChild(xmlOldEle)

xdoc.SelectSingleNode("//div").PrependChild(xmlNewEle)

================================================== =============
Cerebus,
Im not that hot on using XmlDocumentFragment, but would like to learn
more - any good resources on this?

Mar 3 '06 #3
wow,
really thanks guys I'm also solved - I'm basically C# programmer.

main function which I used here is : ReplaceChild() and AppendChild()
and other part of coding which I need to do.

& once again thanks to Cerebrus & dickster.

XmlElement newcatalogentry =
xmldoc.CreateElement("TABLE");
XmlAttribute tblwidthattr =
xmldoc.CreateAttribute("width");
tblwidthattr.Value = "100%";

newcatalogentry.SetAttributeNode(tblwidthattr);
XmlAttribute tblcspaceattr =
xmldoc.CreateAttribute("cellspacing");
tblcspaceattr.Value = "0";

newcatalogentry.SetAttributeNode(tblcspaceattr);
XmlAttribute tblcpadattr =
xmldoc.CreateAttribute("cellpadding");
tblcpadattr.Value = "0";

newcatalogentry.SetAttributeNode(tblcpadattr);

XmlAttribute newcatalogattr =
xmldoc.CreateAttribute("ID");
newcatalogattr.Value = "custctltbl2_1";

newcatalogentry.SetAttributeNode(newcatalogattr);
XmlElement firstelement =
xmldoc.CreateElement("TR");
newcatalogentry.AppendChild(firstelement);
XmlAttribute trtxtwidthattr =
xmldoc.CreateAttribute("width");
trtxtwidthattr.Value = "100%";

firstelement.SetAttributeNode(trtxtwidthattr);
XmlElement tdxe =
xmldoc.CreateElement("TD");
XmlAttribute tdtxtwidthattr =
xmldoc.CreateAttribute("width");
tdtxtwidthattr.Value = "100%";
tdxe.SetAttributeNode(tdtxtwidthattr);
firstelement.AppendChild(tdxe);
XmlNode xnode = xnod.Clone();
tdxe.AppendChild(xnode);
XmlElement lstelmt =
xmldoc.CreateElement("SELECT");
XmlAttribute sltwidthattr =
xmldoc.CreateAttribute("style");
sltwidthattr.Value = "width:91%";
lstelmt.SetAttributeNode(sltwidthattr);

XmlAttribute selidattr =
xmldoc.CreateAttribute("ID");
selidattr.Value = "custsugg" + id;
lstelmt.SetAttributeNode(selidattr);
XmlAttribute selclsattr =
xmldoc.CreateAttribute("classname");
selclsattr.Value = "drp";
lstelmt.SetAttributeNode(selclsattr);
XmlAttribute selsizeattr =
xmldoc.CreateAttribute("size");
selsizeattr.Value = "4";
lstelmt.SetAttributeNode(selsizeattr);
XmlElement tdlstelmt =
xmldoc.CreateElement("TD");
XmlAttribute tdsltwidthattr =
xmldoc.CreateAttribute("width");
tdsltwidthattr.Value = "100%";
tdlstelmt.SetAttributeNode(tdsltwidthattr);
tdlstelmt.AppendChild(lstelmt);

XmlElement trlstelmt =
xmldoc.CreateElement("TR");
XmlAttribute tridattr =
xmldoc.CreateAttribute("ID");
tridattr.Value = "custsugg" + id;
trlstelmt.SetAttributeNode(tridattr);

XmlAttribute trsltwidthattr =
xmldoc.CreateAttribute("width");
trsltwidthattr.Value = "100%";
trlstelmt.SetAttributeNode(trsltwidthattr);
XmlAttribute trposatt =
xmldoc.CreateAttribute("style");
trposatt.Value =
"position:absolute;display:none;visibility:hidden" ;
trlstelmt.SetAttributeNode(trposatt);
trlstelmt.AppendChild(tdlstelmt);
newcatalogentry.AppendChild(trlstelmt);

divnod.ReplaceChild(newcatalogentry, xnod);

Mar 3 '06 #4
Hi Murali,

I personally find it much more concise and efficient to simply load an
XML string (where it is not known beforehand), and then make inserts /
removals from the Main document, rather than adding elements and
attributes individually. It also uses far less variables and there is
less room for confusion. You might experiment with both methods.

Dickster,

Your solution will surely do the job, but it might be more complicated
to add attributes to the "table" element.
Im not that hot on using XmlDocumentFragment, but would like to learn
more - any good resources on this?


Well, using an XmlDocumentFragment is pretty simple. (Note: I'm talking
about VS 2003)
1. Instantiate it using the CreateDocumentFragment() method of the
XmlDocument
2. Set it's InnerXml property to the XML markup of the fragment you
want to insert / replace etc.
3. Use it wherever you can use an XmlNode, since it inherits from
XmlNode.

As for resources, I just use the MSDN help. You might search online for
some good tutorials.

HTH,

Regards,

Cerebrus.

Mar 3 '06 #5
Thanks Cerebus

XmlNode is abstract - XmlDocument, XmlElement & XmlDocumentFragment
all inherit from it. Which is fine

What I can not get my head round is that XmlElement objects have to
come from a XmlDocument Context..

Consider this code:
----------------------------------------------------------
Dim xdoc As New XmlDocument
xdoc.LoadXml("<table></table>")
Dim xmlEle As XmlElement = xdoc.CreateElement("tr")

But I cant then say:

xmlNewEle.AppendChild(<< A New XmlElement object which would be a 'td'
)


Any thoughts?

Mar 3 '06 #6
> load an
XML string (where it is not known beforehand), and then make inserts /
removals from the Main document, rather than adding elements and
attributes individually


Hi Cerebrus,

Can you expain with sample code, then it is clear for me.

bye
murali@pune

Mar 4 '06 #7
Hi Murali,

The sample code is there in my reply to your original post. Did you try
it ?

Regards,

Cerebrus.

Mar 4 '06 #8
The XmlDocument type has a method

..LoadXml()

You can either load an xml file by passing in the path

..LoadXml("path to xml file")

or XML as a string

..LoadXml("<table></table>")

Mar 6 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Wolfgang Schwanke | last post by:
Dear usenet, I'm having the following small problem. I've been ask to add some Quicktime panoramas to a website. The author of the panoramas has made two versions of each: One in MOV format,...
3
by: Tony Sutton | last post by:
I have this following code in my html file: <object classid="clsid:166B1BCA-3F9C-11CF-8075-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#...
11
by: Anna | last post by:
Hi all. I want to embed the EMBED tag in the object tag. I understood that I need to provide a PARAM tag inside the OBJECT whose value will hold the content of EMBED src attribute, but after...
2
by: François de Dardel | last post by:
Please don't scream that EMBED is evil ! What I am doing is <EMBED SRC="BWV659.mid" AUTOSTART="false" LOOP="TRUE" CONTROLS="SMALLCONSOLE" WIDTH="50" HEIGHT="15" TITLE="BWV 659"> Note the...
2
by: Cris Curtis | last post by:
When I use an embed tag that uses a dynamic aspx page, the dynamic aspx page appears to get called 2 times instead. Below is code that adds an embed tag to a placeholder control that will use...
8
by: Cowan Services | last post by:
My apologies if this has been covered before, but I was unable to download messages before 11/23/05. I am trying to bring some HTML pages w/embedded .WMV files into W3C compliance. Right now,...
1
by: Andrew Poulos | last post by:
With "normal" SWF HTML there's an EMBED tag nested within an OBJECT tag. How can I check which tag is actually displaying the SWF? I'm using CSS on them and the style on the OBJECT affects the...
6
by: qualitychecker | last post by:
Hello thanks for your help.. I try to reference a PHP module from within a HTML file, see below ---------------------------------------- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">...
2
dream party
by: dream party | last post by:
Inserting a Flash (SWF, FLV) file into HTML web page is already an old and familiar thing to all of us. It is a rather non-flexible thing that just to edit some options in the template. However, I...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.