Entity Will Not Save XML

  • Thread starter Thread starter Mike_CuttingEdge
  • Start date Start date
M

Mike_CuttingEdge

Guest
Hi: I am trying to save a xml string to the database using entity framework. For some reason I am getting an error when the context.SaveChanges() is used.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


//using System.Xaml;
//using System.Xml;
using System.Xml.Linq;

namespace MyLinqtoXmlTest
{
/// <summary>
/// This project uses Linq to Xaml to:
/// 1) Open & Load the Xml file. Use Button for 1.
/// 2) Display the original file in the OriginalXml window. Use Button for 2
/// 3) Go to the DrawingGroup.Children.
/// 4) Insert in 3 places the new GeometryDrawing or Path. Use Button for 3 & 4.
/// 5) Display the modified file in the ModifiedXml window. Use Button for 5.
/// 6) Save & Close the modified file. Use Button for 6.
/// 7) Use Button btnGetFromDB to get xml information from database.
/// 8) Use Button btnSaveToDB to save xml information to the database.
/// Note: There are 2 approachs to Linq to Xml
/// 1) In Memory Tree Modification
/// 2) Functional Construction
/// </summary>
public partial class MainWindow : Window
{
//this path does not work
// private String dataPath = @"C:\Users\Mike\documents\visual studio 2010\Projects\MyLinqtoXmlTest\MyLinqtoXmlTest\"; //this is the path for the file
private String dataPath = @"C:\Users\Mike Gallinger\Documents\Visual Studio 2010\Projects\MyLinqtoXmlTest\MyLinqtoXmlTest\";
private String xmlFileName = "HorizontalPipe.xaml";
private XNamespace ns;

private XDocument docOriginal;
//display windows
private OriginalXml wOriginal = new OriginalXml();
private ModifiedXml wModified = new ModifiedXml();

//Data Base Code

//member variables
private MyTestDBEntities context = new MyTestDBEntities();
private List<XmlTestTable> listOfXmlTestTable;
//public properties
public List<XmlTestTable> ListOfXmlTestTable
{
get
{
return listOfXmlTestTable;
}
set
{
listOfXmlTestTable = value;
}
}
public String EquipmentTypeName
{
get;
set;
}
// because of entity framework the xaml stored in the xml feild of the table is returned to C# as a string
public String StringXmlFromDatabase
{
get;
set;
}


//End Database Code




public MainWindow()
{
InitializeComponent();

}

private void btnLoadXml_Click(object sender, RoutedEventArgs e)
{
//load the xaml document from a file
docOriginal = XDocument.Load(dataPath + xmlFileName);
//get the namespace from the document
ns = docOriginal.Root.Name.NamespaceName;
//C:\Users\Mike Gallinger\Documents\Visual Studio 2010\Projects\MyLinqtoXmlTest\MyLinqtoXmlTest\HorizontalPipe.xaml

}

private void btnDisplayOriginal_Click(object sender, RoutedEventArgs e)
{
// wOriginal.textBlockOriginal.Text = docOriginal.ToString();
wOriginal.textBlockOriginal.Text = docOriginal.ToString();
wOriginal.Show();
}
private XElement CreateNewElement()
{
// To make the vertical line below with the Sample 1
XElement myInsert = new XElement(ns + "GeometryDrawing",
new XAttribute("Brush", "#FFFFFFFF"),
new XAttribute("Geometry", "F1 M 25.0417,0.0766792L 25.0417,50.0767"),
new XElement(ns + "GeometryDrawing.Pen",
new XElement(ns + "Pen",
new XAttribute("Thickness", "3"),
new XAttribute("LineJoin", "Round"),
new XAttribute("Brush", "#FF000000"))));
return myInsert;
}
private List<XElement> GetParentElement()
{

//use linq to xml to get the <DrawingGroup.Children> parent element that we will add to
List<XElement> parentElementList = (from parent in docOriginal.Elements(ns + "ResourceDictionary").Descendants(ns + "DrawingBrush")
.Elements(ns + "DrawingBrush.Drawing").Elements(ns + "DrawingGroup").Elements(ns + "DrawingGroup.Children")
select parent).ToList();

return parentElementList;

}
private void btnInsert_Click(object sender, RoutedEventArgs e)
{

//create the new element to insert
XElement newElement = CreateNewElement();
//get the parent element
List<XElement> parentElementList = GetParentElement();

//add the new element to the parent elements in the list
parentElementList[0].Add(newElement);
parentElementList[1].Add(newElement);
parentElementList[2].Add(newElement);

}

private void btnDisplayModified_Click(object sender, RoutedEventArgs e)
{
// wOriginal.textBlockOriginal.Text = docOriginal.ToString();
wModified.textBlockModified.Text = docOriginal.ToString();
wModified.Show();


}
//save the modified file
private void btnSave_Click(object sender, RoutedEventArgs e)
{
//save the file with the new elements added to it
docOriginal.Save(dataPath + xmlFileName);
}

private void btnExit_Click(object sender, RoutedEventArgs e)
{
if (MessageBox.Show("Are you sure you want to close?", "Annoying Prompt", MessageBoxButton.YesNo, MessageBoxImage.Question)
== MessageBoxResult.Yes)
mainWindow.Close();
}
//gets the xml from the database MyTestDB
private void btnGetFromDB_Click(object sender, RoutedEventArgs e)
{

try
{
//this gets the row from the table containing the station object name the user puts in the text box
//we then get the xml contained in that row and display it in ModifiedXmlWindow
var theEquipType = from q in context.XmlTestTables
where q.StationObjectName == txtbxNewName.Text
select q;
//enumerate through the data and put the results in public properties that will be read by StationObject class
foreach (var t in theEquipType) //there will only be 1 row
{
//load the string from the XmlResourceFile field to the modified window
wModified.textBlockModified.Text = t.XmlResourceFile;//this returns a string from the table
wModified.Show();

}

}
catch
{
MessageBox.Show("The Get Xml table query failed!");
}




}
//modify xaml code already in the database
//Step 1 get the xml from the database
//Step 2 put a new element in the xml
//Step 3 save the xml back to the database in the same station object

private void btnModifyToDB_Click(object sender, RoutedEventArgs e)
{
try
{
//Step 1************************ Same code as btnGetFromDB_Click
//this gets the row from the table containing the station object name the user puts in the text box
//we then get the xml contained in that row so we can modify its xml
var theEquipType = from q in context.XmlTestTables
where q.StationObjectName == txtbxNewName.Text
select q;
//enumerate through the data and put the results in public properties that will be read by StationObject class
foreach (var t in theEquipType) //there will only be 1 row
{
//load the string from the XmlResourceFile field to the XmlFromDatabase public property
StringXmlFromDatabase = t.XmlResourceFile.ToString();
//convert the string to xaml by parseing THIS IS CRITICAL
docOriginal = XDocument.Parse(@StringXmlFromDatabase); //XElement.Parse(@StringXmlFromDatabase);
//get the namespace from the document
ns = docOriginal.Root.Name.NamespaceName;

}
//Step 2*************************** Modify the string
//create a new element to add
XElement newElement = CreateNewElement();
//get the parent element from docOriginal which is obtained from the database above
List<XElement> parentElementList = GetParentElement();

//add the new element to the parent elements in the list
parentElementList[0].Add(newElement);
parentElementList[1].Add(newElement);
parentElementList[2].Add(newElement);




//Step 3************************** Same code as btnSaveToDB_Click
//this creates a variable and gets the new equipment type information
//from the station object in StationObjectCreationUtility
//it then sends the information to the data base
var newEquipType = new XmlTestTable();
//save the new station objects name from the text box
newEquipType.StationObjectName = txtbxNewName.Text;

//then save the modified xml file to the XmlResourceFile field
newEquipType.XmlResourceFile = docOriginal.ToString();

//add test to TestString to checkout the add method to the database
String test = "The modified file.";
newEquipType.TestString = test;

context.XmlTestTables.AddObject(newEquipType);

//context.AddToXmlTestTables(newEquipType);

context.SaveChanges();




}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "The Modify Xml to database failed!");
}



}
//saves the xml to the database MyTestDB
private void btnSaveToDB_Click(object sender, RoutedEventArgs e)
{
try
{
//this creates a variable and gets the new equipment type information
//from the station object in StationObjectCreationUtility
//it then sends the information to the data base
var newEquipType = new XmlTestTable();
//save the new station objects name from the text box
newEquipType.StationObjectName = txtbxNewName.Text;

//then save the original xml file to the XmlResourceFile field
newEquipType.XmlResourceFile = docOriginal.ToString();

//add test to TestString to checkout the add method to the database
String test = "This is a test.";
newEquipType.TestString = test;

context.XmlTestTables.AddObject(newEquipType);
//context.AddToXmlTestTables(newEquipType);

context.SaveChanges();

}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString(),"The Save Xml table query failed!");

}


}


}
}


The page is:

<Window x:Name="mainWindow" x:Class="MyLinqtoXmlTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:MyNameSpace="clr-namespace:MyLinqtoXmlTest"
Title="MainWindow" Height="400" Width="525" Background="Green
<Grid>
<StackPanel Height="289" HorizontalAlignment="Left" Margin="23,10,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="149
<Button Content="Load Xml File" Height="33" Name="btnLoadXml" Click="btnLoadXml_Click" />
<Button Content="Display original file." Height="33" Name="btnDisplayOriginal" Click="btnDisplayOriginal_Click" />
<Button Content="Insert Xml Elements" Height="33" Name="btnInsert" Click="btnInsert_Click" />
<Button Content="Display modified file." Height="33" Name="btnDisplayModified" Click="btnDisplayModified_Click" />
<Button Content="Save modified file." Height="33" Name="btnSave" Click="btnSave_Click" />
<Button Content="Get Xml from Database" Height="33" Name="btnGetFromDB" Click="btnGetFromDB_Click" />
<Button Content="Modify Xml to Database." Height="33" Name="btnModifyToDB" Click="btnModifyToDB_Click" />
<Button Content="Save Xml to Database." Height="33" Name="btnSaveToDB" Click="btnSaveToDB_Click" />
<Button Content="Exit" Height="23" Name="btnExit" Click="btnExit_Click" Background="#FFB13434" />
</StackPanel>
<Label Content="New Name:" Height="28" Name="label1" Background="Yellow" Margin="264,257,159,76" />
<TextBox Height="30" HorizontalAlignment="Left" Margin="359,255,0,0" Name="txtbxNewName" VerticalAlignment="Top" Width="132" />
<TextBlock TextWrapping="Wrap" Height="111" HorizontalAlignment="Left" Margin="243,31,0,0" Name="textBlock1" Text="User: Loads a xaml file with Load Xml File. Then display original file, insert element, display modified file. Or in text box type in name then save xml to database, then get xaml from database." VerticalAlignment="Top" Width="188" Background="Blue" />
</Grid>
</Window>

The data base is just a simple table with 3 fields StationObjectName is nchar, XmlResourceFile is xml and TestString is nchar. The TestString saves okay by itself but the problem seems to be with the xml field. Thanks in advance.

a3b6d2594bf84448bde3fe66c4ab87fc._.jpg





Mike Gallinger C.Tech. Cutting Edge Computing Software Developer

Continue reading...
 
Back
Top