|
You can load the xml in to a XmlDocument object and do the changes
in it by using the Select single node method. And finally save the document.
Gokul
|
|
|
|
|
hi there,
i have 2 functions: the one method (serialize) must create a new xmldocument and call the other method (getxml), which will create and return an xmlnode. the problem is i keep getting the same error message: "The node to be inserted is from a different document context."
i don't want to pass the whole xmldocument to the method because of performance reasons.
here is the code, can anyone help?
public void Serialize()
{
//create xmldoc and call control's getXML method to add their xml to doc
XmlDocument mainDocument = new XmlDocument();
XmlElement rootElement = mainDocument.CreateElement("Root");
mainDocument.AppendChild(rootElement);
XmlNode root = mainDocument.DocumentElement;
foreach (OurTextbox i in this.Controls)
{
mainDocument.AppendChild(i.GetXML());
}
mainDocument.Save("c:\\output.xml");
}
public XmlNode GetXML()
{
XmlDocument doc = new XmlDocument();
XmlElement nameElement = doc.CreateElement(this.Name.ToString());
//create child elements
XmlElement sizeElement = doc.CreateElement("Size");
XmlElement locationElement = doc.CreateElement("Location");
//create size attributes
XmlAttribute widthAttribute = doc.CreateAttribute("Width");
widthAttribute.InnerText = System.Convert.ToString(this.Size.Width);
XmlAttribute heightAttribute = doc.CreateAttribute("Height");
heightAttribute.InnerText = System.Convert.ToString(this.Size.Height);
//create location attributes
XmlAttribute xAttribute = doc.CreateAttribute("X");
xAttribute.InnerText = System.Convert.ToString(this.Location.X);
XmlAttribute yAttribute = doc.CreateAttribute("Y");
yAttribute.InnerText = System.Convert.ToString(this.Location.Y);
//add size attributes to size element
sizeElement.Attributes.Append(widthAttribute);
sizeElement.Attributes.Append(heightAttribute);
//add location attributes to location element
locationElement.Attributes.Append(xAttribute);
locationElement.Attributes.Append(yAttribute);
//add size and location element to name element
nameElement.AppendChild(sizeElement);
nameElement.AppendChild(locationElement);
//rootNode.AppendChild(nameElement);
return nameElement;
}
|
|
|
|
|
Use "ImportNode":
XmlNode newNode = doc.ImportNode(doc2.DocumentElement.LastChild, true);
doc.DocumentElement.AppendChild(newNode);
|
|
|
|
|
Hi All,
I am reading data from SQL Server into a dataset the date format it get into the dataset is same as in the DB(MM/dd/YYYY) but when I use the GetXml() function to get the dataset into string it is changing the date format from “MM/dd/YYYY hh:mm:ss am/pm” (Example: 12/30/2005 12:00:00 AM) to its own format which is “yyyy-MM-ddThh:mm:ss.0000000+03:00 ” (Example: 2005-12-30T00:00:00.0000000+03:00)
Can some body suggest me how can we covert it to the users web culture format or any other user defined format?
|
|
|
|
|
The “yyyy-MM-ddThh:mm:ss.0000000+03:00” format is actually a dateTime.tz data type. It is a subset of the ISO 8601 format with optional time and optional zone. Fractional seconds can be as precise as nanoseconds! Anyway, here is a C++/CLI example to convert this data type into a DateTime variable where the date-time is stored in an attribute called published within an element called book:
<br />
String ^xml = <br />
"<?xml version='1.0'?>"<br />
"<books>"<br />
"<book published='2005-12-30T00:00:00.0000000+03:00'/>"<br />
"<book published='2005-12-30T00:00:00.0000000-05:00'/>"<br />
"</books>"<br />
<br />
XmlDocument ^doc = gcnew XmlDocument();<br />
doc->LoadXml(xml);<br />
<br />
XmlNodeList ^nodes = doc->SelectNodes("/books/book");<br />
<br />
for each (XmlNode ^node in nodes)<br />
{<br />
DateTime published =<br />
XmlConvert::ToDateTime(<br />
node->Attributes->GetNamedItem("published")->Value,<br />
XmlDateTimeSerializationMode::Utc);<br />
<br />
Console::WriteLine(published);<br />
}<br />
|
|
|
|
|
Hi,
Thanks for the reply, Iam getting a dataset from a SQL query and later Iam using the "GetXml()" to get the dataset in a string. in this string I dont know which node has the date, it depends upon the query.
My main issue is first I want to search which node in the string has the date value in this format ( sample date 2006-12-25T16:30:00.0000000+03:00) and then format it as dd/MM/yyyy or any other format.
and more over I am using C# to achive this functionality.
I have modified your code as follows
DataSet myDs = new DataSet();
myDs = myDataset();
string strxml = myDs.GetXml();
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(strxml);
XmlNodeList nodelst = xdoc.SelectNodes("/NewDataSet/Table/StartDate");
foreach (XmlNode node in nodelst)
{
DateTime dt = XmlConvert.ToDateTime(node.InnerText);
Response.Write("Date :" + dt.ToString("dd/MM/yyyy") + "\n");
}
My main concern is in the "xdoc.SelectNodes("/NewDataSet/Table/StartDate");"
I dont want to give the path or the element itself.
I hope I've made my point clear, please feel free if u have any issues
Thanks in Advance
|
|
|
|
|
Please help me... I have a real brain boggler of a problem. This is the code i'm using...
<xsl:template match="TableEndOfDay">
<table border="1" cellspacing="0" cellpadding="1" width="100%">
<tr>
<td>
<table border="1" cellspacing="0" cellpadding="1" width="100%">
<xsl:for-each select="StaffID">
<tr>
<th><font face="Tahoma">hallo</font></th>
</tr>
</xsl:for-each>
</table>
</td>
</tr>
</table>
</xsl:template>
As Soon as i take out the xsl:for-each statement, the "hallo" displays once in the tables.
BUT as soon as i use the code as it is, the only thing showing is the outer tables' border. I need to be able to display the "hallo" once each time for each StaffID. It is quite urgant to me. Please help.....
|
|
|
|
|
You need to post some of the XML you are trying to transform. However, the "StaffID" elements must be child nodes of "TableEndOfDay" in order for the for-each to work as expected. I assume it is finding an empty node-set and the select condition is returning false.
-- modified at 9:54 Friday 10th February, 2006
|
|
|
|
|
Here is a snippet of the XML...
<?xml version="1.0"?>
<?xml-stylesheet type='text/xsl' href='EndOfDayReport.xsl'?>
<EndOfDayReport>
<Heading>
<EndOfDayReportOpskrif>End of Day/Month Report</EndOfDayReportOpskrif>
<ReportPeriod>Report Period: 24 November 2005 - 25 November 2005</ReportPeriod>
<DateCreated>Date Created: 2006/02/10 03:43:48 PM</DateCreated>
</Heading>
<TableEndOfDay>
<StaffID xmlns="2">
<StaffName>Pieter</StaffName>
<Services>
<ServiceID xmlns="1">
<ServiceKey>BW.1</ServiceKey>
<ServiceDescription>BLOW WAVE - SHORT</ServiceDescription>
<Qty>1</Qty>
<Percent>1%</Percent>
<Amount>140.00</Amount>
</ServiceID>
<ServiceID>
<ServiceKey>BW.2</ServiceKey>
<ServiceDescription>BLOW WAVE - MEDIUM</ServiceDescription>
<Qty>0</Qty>
<Percent>0%</Percent>
<Amount>0</Amount>
</ServiceID>
|
|
|
|
|
XML (Note: I removed "xmlns" and replace it with "id", since no real namespace or valid namespace was defined.):
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="EndOfDayReport.xsl"?>
<EndOfDayReport>
<heading>
<EndOfDayReportOpskrif>End of Day/Month Report</EndOfDayReportOpskrif>
<ReportPeriod>Report Period: 24 November 2005 - 25 November 2005</ReportPeriod>
<DateCreated>Date Created: 2006/02/10 03:43:48 PM</DateCreated>
</heading>
<tableEndOfDay>
<StaffID id="2">
<StaffName>Pieter</StaffName>
<Services>
<ServiceID id="1">
<ServiceKey>BW.1</ServiceKey>
<ServiceDescription>BLOW WAVE - SHORT</ServiceDescription>
<Qty>1</Qty>
<Percent>1%</Percent>
<Amount>140.00</Amount>
</ServiceID>
<ServiceID>
<ServiceKey>BW.2</ServiceKey>
<ServiceDescription>BLOW WAVE - MEDIUM</ServiceDescription>
<Qty>0</Qty>
<Percent>0%</Percent>
<Amount>0</Amount>
</ServiceID>
</Services>
</StaffID>
</tableEndOfDay>
</EndOfDayReport>
XSLT (Note: the main template searches selects the "DocumentElement" and the "for-each" searches its child nodes):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method='html' version='1.0' encoding='UTF-8' indent='yes'/>
<xsl:template match="/EndOfDayReport">
<html>
<head>
<title>Table End of Day</title>
</head>
<body>
<table border="1" cellspacing="0" cellpadding="1" width="100%">
<tr>
<td>
<table border="1" cellspacing="0" cellpadding="1" width="100%">
<xsl:for-each select="tableEndOfDay/StaffID">
<tr>
<th><font face="Tahoma">Hello, <xsl:value-of select="StaffName"/></font></th>
</tr>
</xsl:for-each>
</table>
</td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
-- modified at 15:02 Monday 13th February, 2006
|
|
|
|
|
Thanx, i'll try it. Enjoy your day.
|
|
|
|
|
How does XSLTransform work with the resolver using C#?
Apparantly the TransForm(string input, string output) is obsolute.
must use Transform(stirng input, string output, Resolver resolver)
Have an xml and xsl file which I want to convert to html
Thanks in advance
|
|
|
|
|
You use the XmlResolver to resolve external XML resources such as entities and schemas.
C#:
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = CredentialCache.DefaultCredentials;
XslTransform xsl = new XslTransform();
// Use to resolve xsl:import and xsl:include elements
// The resolver is not cached after Load is completed
// If "null", a default resolver is used and exteral resources are not resolved.
// xsl.Load("filename.xsl", null);
xsl.Load("filename.xsl", resolver);
// If "null", a default resolver is used and exteral resources are not resolved.
// xsl.Transform("filename.xml", "filename.htm", null);
xsl.Transform("filename.xml", "filename.htm", resolver);
Note: This information was taken from the .NET Framework Class Library
|
|
|
|
|
Thanx, works like a bomb now. I just don't understand why they made the
xsl:Transform(string,string) obsolute, why not just add the one that includes the resolver?
Anyway, thanx once again.
|
|
|
|
|
Hey Guys need help
Previously i convert a XML File to A String
The String Contain all the information Like "<ns0:Temp><Name><FirstName>Brandon</FirstName></Name></ns0:Temp>"
How can i convert the string back to XML file? Need Help!!
Thanks a million
KaKaShi HaTaKe
KaKaShi HaTaKe
|
|
|
|
|
<br />
XmlDocument xDoc = new XmlDocument();<br />
XmlElement rootElem = xDoc.CreateElement("root");<br />
rootElem.InnerXml= locXml
xDoc.AppendChild(rootElem);<br />
sathy
|
|
|
|
|
You have to account for the namespace in the string!
<br />
String xml = "<?xml version='1.0'?>" +<br />
"<ns0:Temp xmlns:ns0='www.mydomain.com/misc'>...</ns0:Temp>";<br />
<br />
XmlDocument doc = new XmlDocument();<br />
doc.LoadXml(xml);<br />
-- modified at 11:18 Friday 10th February, 2006
|
|
|
|
|
Hi;
I've just started playing with SQLXML 4.0, here is the code which I am using to load the XML into database.
SQLXMLBulkLoad4Class bulkLoad = new SQLXMLBulkLoad4Class();
bulkLoad.ConnectionString = connString;
bulkLoad.BulkLoad = true;
bulkLoad.KeepIdentity = false;
bulkLoad.XMLFragment = true;
bulkLoad.Execute("C:\books.xsd", "C:\books.xml");
My question is that, the mapping file i.e. xsd or booksmapping.xml is compulsory to pass or we can have that information into xml data file? We want to avoid two files to maintain.
Thanks
M. Essa Mughal
Toronto, CANADA
Email: essamughal@yahoo.com
|
|
|
|
|
You can embed the schema in the XML file. However, your XML file increases in size and the schema is harder to maintain since it is embedded into each instance of your books.xml file.
|
|
|
|
|
Hi Jackson;
I am having another problem here. I created the xml file and xsd file using Datatable method WriteXMLSchema and its given schema is not working with SQLXML bulk load.
Here are two Schema:
Working schema which I got from example:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
<xsd:element name="Book" sql:relation="tblBooks">
<xsd:complextype>
<xsd:sequence>
<xsd:element name="Title" type="xsd:string" sql:field="BookTitle">
<xsd:element name="Publisher" type="xsd:string">
<xsd:element name="DateOfPurchase" type="xsd:date">
Here is the schema which is generated by Datatable method. It is not working with the xml file:
<xs:schema id="Books" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Books" msdata:isdataset="true" msdata:maindatatable="Book" msdata:usecurrentlocale="true">
<xs:complextype>
<xs:choice minoccurs="0" maxoccurs="unbounded">
<xs:element name="Book">
<xs:complextype>
<xs:sequence>
<xs:element name="Row_ID" msdata:readonly="true" msdata:autoincrement="true" type="xs:int">
<xs:element name="BookTitle">
<xs:simpletype>
<xs:restriction base="xs:string">
<xs:maxlength value="100">
<xs:element name="Publisher" minoccurs="0">
<xs:simpletype>
<xs:restriction base="xs:string">
<xs:maxlength value="100">
<xs:element name="DateOfPurchase" type="xs:dateTime" minoccurs="0">
<xs:unique name="Constraint1" msdata:primarykey="true">
<xs:selector xpath=".//Book">
<xs:field xpath="Row_ID">
Any Idea? Is there any particular format for the schema for SQLXML?
Thanks
M. Essa Mughal
Toronto, CANADA
|
|
|
|
|
Unfortunately, I cannot see your information. I haven't worked with SQLXML; however, I believe the schema produced by the DataTable method would be useless in this situation. A schema is a set of rules that describes how XML is structured. Thus, your XML file must have the structure dictated by SQLXML.
Are you using the ExecuteXmlReader method of the ADO.NET SqlCommand object to take a FOR XML result set and moving it directly into a XmlDataDocument object? Using a XmlDataDocument object, you have the advantage of updating your database via a dataset and still have the original XML from the server.
-- modified at 15:53 Friday 10th February, 2006
|
|
|
|
|
Hey,
I want to beable to generate an XML document describing my textbox's properties, button or any other control. Can anyone give me the code or help? I tried to use the System.Xml.Serialize but it says it does not support buttons.gettype method
-- modified at 2:20 Thursday 9th February, 2006
|
|
|
|
|
am using the current code to make my script compatible to firefox and netscape.....but its not workin...can som1 suggest an alternative
if (window.ActiveXObject)
{flag=0;
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load(cool)
if (xmlDoc.parseError.errorCode != 0)
{
var myError = xmlDoc.parseError;
}
else
{
parse1(xmlDoc.documentElement.childNodes(0).text,xmlDoc.documentElement.childNodes(1).text)
}
}
else if(document.implementation && document.implementation.createDocument)
{flag=1;
var xmlDoc=document.implementation.createDocument("","doc",null);
xmlDoc.async=false;
loaded=xmlDoc.load(cool);
if(!loaded)
{
alert("Error");
}
else
{//alert("success")
alert(xmlDoc.documentElement.childNodes.item(1).childNodes.item(0).nodeValue);
xmlDoc.onload=parse1(xmlDoc.documentElement.childNodes.item(1).childNodes.item(0).nodeValue,"http://www-scf.usc.edu/~csci571/2006Spring/hw4/hummer.xml");
}
}
}
function parse1(file1,file2)
{
if(flag==0)
{
var doc=new ActiveXObject("Microsoft.XMLDOM")
doc.async="false"
doc.load(file1)
var doc1=new ActiveXObject("Microsoft.XMLDOM")
doc1.async="false"
doc1.load(file2)
}
else
var xml=document.implementation.createDocument("","doc1",null);
//alert(xml)
xml.async=false;
loaded=xml.load(file1);
var xml1=document.implementation.createDocument("","doc2",null);
xml1.async=false;
loaded2=xml1.load(file2);
|
|
|
|
|
Hello all, i have a windows application which needs a small database (each
record has 7 items) and i am thinking as using xml as database. I will need to
add, delete, update the records (and show them ofcourse in a datagrid). Is there
any documentation (article or sample code) to look at?
Thanks a lot
Knowledge is a power.
|
|
|
|
|
Using xml as database in the simplest sense is basically storing data as text file in xml format (that is, unless you're using SQL Server 2005 to store your xml).
What you need to learn is XPath, and more specifically XQuery. These are comparable to understanding SQL syntax to manipulate database.
The XmlDocument class provides you several functions to load and save text files, and to manipulate xml nodes within.
I believe the ToDoList[^] application in CodeProject uses Xml to store data, if you want to look at a sample project.
Hope that helps
Edbert
Sydney, Australia
"A day without sunshine is like, you know, night."
-- modified at 0:39 Tuesday 7th February, 2006
|
|
|
|