I am trying to display a portion of an xml file in a data grid. I need to transform a number of elements to attribute to display in the grid. If i delete all of the additional elements in the xml file, my code works. however i need those elements to remain
in the XML file. So, here is what i got, maybe someone can help:
I got this to work, sort of... but it does not insert the element to the end of Status elements, but rather after the first Status element:
Dim xDoc = XDocument.Load(Server.MapPath("~/App_Data/Appointments.xml"))
xDoc.Element("Appointments").Element("Resources").Element("Status").AddAfterSelf(New XElement("Status", New XElement("Key", "3"), New XElement("Text", "Oops")))
How do i get it to save after the last Status Element or at least reorder the Status elements based on the Key Child element within the Status element after the new Status element has been created?
None
0 Points
2 Posts
The specified node cannot be inserted as the valid child of this node, because the specified node...
Jul 15, 2010 02:10 PM|fligles|LINK
I am trying to display a portion of an xml file in a data grid. I need to transform a number of elements to attribute to display in the grid. If i delete all of the additional elements in the xml file, my code works. however i need those elements to remain in the XML file. So, here is what i got, maybe someone can help:
Working XML file (not useful):
<?xml version="1.0" encoding="utf-8"?>
<Appointments>
<Resources>
<Status>
<Key>1</Key>
<Text>Open</Text>
</Status>
<Status>
<Key>2</Key>
<Text>Closed</Text>
</Status>
</Resources>
</Appointments>
Not working xml (need to have it work):
<?xml version="1.0" encoding="utf-8"?>
<Appointments>
<Resources>
<Status>
<Key>1</Key>
<Text>Open</Text>
</Status>
<Status>
<Key>2</Key>
<Text>Closed</Text>
</Status>
</Resources>
<NextID>3</NextID>
<Appointment>
<ID>1</ID>
<Subject>gnfgnnfgng</Subject>
<Description>Last one, for now</Description>
<Start>2010-07-14T10:30Z</Start>
<End>2010-07-14T11:00Z</End>
<Resources>
<Status Key="1" />
</Resources>
</Appointment>
<Appointment>
<ID>2</ID>
<Subject>ki;uy;</Subject>
<Description>
</Description>
<Start>2010-07-07T00:00Z</Start>
<End>2010-07-08T00:00Z</End>
</Appointment>
</Appointments>
XSL File (working, sort of...):
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output version="1.0" method="xml" indent="yes" />
<xsl:template match="Appointments/Resources">
<xsl:element name="Resources">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="Status">
<xsl:element name="Status">
<!-- convert elements to attributes -->
<xsl:for-each select="*">
<xsl:attribute name="{name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each >
</xsl:element>
</xsl:template>
</xsl:stylesheet>
ASPX Page:
<div>
<asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="False"
DataSourceID="XmlDataSource1">
<Columns>
<asp:BoundField DataField="Key" HeaderText="Key" SortExpression="Key" />
<asp:BoundField DataField="Text" HeaderText="Text" SortExpression="Text" />
</Columns>
</asp:GridView>
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/XMLFile2.xml"
TransformFile="~/XSLTFile.xsl"></asp:XmlDataSource>
</div>
Please Help!!!
I got this to work, sort of... but it does not insert the element to the end of Status elements, but rather after the first Status element:
Dim xDoc = XDocument.Load(Server.MapPath("~/App_Data/Appointments.xml"))
xDoc.Element("Appointments").Element("Resources").Element("Status").AddAfterSelf(New XElement("Status", New XElement("Key", "3"), New XElement("Text", "Oops")))
xDoc.Save(Server.MapPath("~/App_Data/Appointments.xml"))
How do i get it to save after the last Status Element or at least reorder the Status elements based on the Key Child element within the Status element after the new Status element has been created?
Star
10552 Points
1998 Posts
Re: The specified node cannot be inserted as the valid child of this node, because the specified...
Jul 17, 2010 12:01 PM|Martin_Honnen|LINK
The following XSLT stylesheet
transforms
into
As for VB problem, I think you want
xDoc.Root.Element("Resources").Add(New XElement("Status", ...))
to add a new Status element at the end of the Resources element.
My blog
None
0 Points
2 Posts
Re: The specified node cannot be inserted as the valid child of this node, because the specified...
Oct 08, 2010 12:50 PM|fligles|LINK
Thanks, worked perfectly