Hi
Today i was trying to read and write data to Xml file but after few success i got stuck to a problem.
Case 1 :
When i didn’t open my Xml in Mono, In this case i can write to my Xml file. But i can’t read the latest element added to my Xml file. If i try to read my Xml i just get the previous data. But if i open my Xml file manually than i can see the added data.
Case 2 :
If i keep the Xml file open in Mono than i get this error but i can see the updated data if i read through the Xml file.
System.IO.IOException: The process cannot access the file ‘C:\Users\Mr.Sushanta\Documents\Bikash Acc Soft\Assets\Resources\Stock.xml’ because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)
at System.IO.FileStream…ctor(String path, FileMode mode, FileAccess access, FileShare share)
at System.IO.FileInfo.Open(FileMode mode, FileAccess access, FileShare share)
at MonoDevelop.Projects.Text.TextFile.Read(FilePath fileName, String encoding) in c:\BuildAgent\work\fc2773206889b0da\monodevelop\main\src\core\MonoDevelop.Core\MonoDevelop.Projects.Text\TextFile.cs:line 123
at MonoDevelop.Projects.Text.TextFile.ReadFile(FilePath fileName, String encoding) in c:\BuildAgent\work\fc2773206889b0da\monodevelop\main\src\core\MonoDevelop.Core\MonoDevelop.Projects.Text\TextFile.cs:line 72
at MonoDevelop.SourceEditor.SourceEditorView.Load(String fileName, String encoding) in c:\BuildAgent\work\fc2773206889b0da\monodevelop\main\src\addins\MonoDevelop.SourceEditor2\MonoDevelop.SourceEditor\SourceEditorView.cs:line 484
at MonoDevelop.SourceEditor.SourceEditorView.Load(String fileName) in c:\BuildAgent\work\fc2773206889b0da\monodevelop\main\src\addins\MonoDevelop.SourceEditor2\MonoDevelop.SourceEditor\SourceEditorView.cs:line 461
at MonoDevelop.SourceEditor.SourceEditorWidget.Reload() in c:\BuildAgent\work\fc2773206889b0da\monodevelop\main\src\addins\MonoDevelop.SourceEditor2\MonoDevelop.SourceEditor\SourceEditorWidget.cs:line 946
This is my Xml
<?xml version="1.0" encoding="utf-8"?> 500 600 200 1000Here is the code i created
#pragma strict
var detailStock : String;
var scrollPosition : Vector2;
var show : boolean;
var addItem : boolean;
var Style : GUIStyle;
var itemName : String = "";
var itemPrice : String = "";
var itemQnty : String = "";
function Start () {
}
function Update () {
if(Input.GetKey(KeyCode.Space))
{
Application.LoadLevel("Stock");
}
if(Input.GetKeyDown(KeyCode.Escape))
{
Application.Quit();
}
}
function OnGUI()
{
GUI.Box(Rect(0,0,1024,550),"",Style);
if(show == false addItem == false)
{
if(GUI.Button(Rect(0,0,100,30),"Show"))
{
ReadXml();
show = true;
}
}
if(addItem == false show == false)
{
if(GUI.Button(Rect(0,40,100,30),"Add"))
{
addItem = true;
}
}
if(addItem == true)
{
GUI.Box(Rect(0,0,300,20),"Enter Iteam Name");
itemName = GUI.TextField (Rect (0, 30, 200, 20), itemName, 25);
GUI.Box(Rect(0,60,300,20),"Enter Price");
itemPrice = GUI.TextField (Rect (0, 90, 200, 20), itemPrice, 25);
GUI.Box(Rect(0,120,300,20),"Enter Qnty");
itemQnty = GUI.TextField (Rect (0, 150, 200, 20), itemQnty, 25);
if(GUI.Button(Rect(0,180,100,20),"Save"))
{
WriteToXmlFile();
addItem = false;
}
}
if(show == true)
{
GUILayout.BeginArea (Rect (0,100,200,500));
scrollPosition = GUILayout.BeginScrollView (scrollPosition, GUILayout.Width (200), GUILayout.Height (100));
GUILayout.Label (detailStock);
if (GUILayout.Button ("Close"))
{
show = false;
detailStock = "";
//textAsset = null;
}
GUILayout.EndScrollView ();
GUILayout.EndArea ();
}
}
function ReadXml()
{
var textAsset : TextAsset = Resources.Load("Stock") as TextAsset;
var xmlDoc : XmlDocument = new XmlDocument();
xmlDoc.LoadXml ( textAsset.text );
var levelsList : XmlNodeList = xmlDoc.GetElementsByTagName("Stock"); // Get Stock
for(var i : int = 0; i < levelsList.Count;i++)
{
var levelInfo : XmlNode = levelsList.Item(i); // getting stock
print(levelInfo.Name); // printing stock node
for(var j : int = 0; j < 1; j++)
{
var levelContent : XmlNodeList = levelInfo.ChildNodes; // getting all the child node inside stock
for(var k : int = 0; k < levelContent.Count; k++)
{
var levelItm : XmlNode = levelContent.Item(k);
print(levelItm.Name); // getting perticular child in stock (like Rice)
detailStock+=levelItm.Name+"\n";
var ItemNameContent : XmlNodeList = levelItm.ChildNodes; // getting child node inside Rice
for(var m : int = 0; m < ItemNameContent.Count ; m++)
{
var ItemNameContentValues : XmlNode = ItemNameContent.Item(m);
if(ItemNameContentValues.Name == "Price")
{
print("Price = " + ItemNameContentValues.InnerText + " RS");
detailStock+=ItemNameContentValues.Name+" ";
detailStock+=ItemNameContentValues.InnerText+" "+"Rs";
}
if(ItemNameContentValues.Name == "Qnty")
{
print("Qnty = " + ItemNameContentValues.InnerText + " Kg");
detailStock+=ItemNameContentValues.Name+" ";
detailStock+=ItemNameContentValues.InnerText+" "+"Kg"+"\n";
}
}
}
}
}
}
function WriteToXmlFile(){
var Asset : TextAsset = Resources.Load("Stock") as TextAsset;
var xmlDoc : XmlDocument = new XmlDocument();
xmlDoc.LoadXml ( Asset.text );
var elmRoot : XmlElement = xmlDoc.DocumentElement;
var elmRootChild : XmlElement = elmRoot.FirstChild;
var iTemName : XmlElement = xmlDoc.CreateElement(itemName);
var iTemPrice : XmlElement = xmlDoc.CreateElement("Price");
iTemPrice.InnerText = itemPrice;
var iTemQnty : XmlElement = xmlDoc.CreateElement("Qnty");
iTemQnty.InnerText = itemQnty;
iTemName.AppendChild(iTemPrice);
iTemName.AppendChild(iTemQnty);
elmRootChild.AppendChild(iTemName);
xmlDoc.Save(Application.dataPath + "/Resources/Stock.xml" );
itemName = "";
itemPrice = "";
itemQnty = "";
}
The code works like this
If i want to Add item to stock than you press Add button , this activate the enter item menu. Where you enter the elements and there values. And press save to save data to Xml file. To save data i am using the Function WriteToXmlFile()
If you want to read data from Xml file you press Show button and this will read your data from Xml and show them in a GUI scroll view.
If you guys can help me to understand what the problem is than this will help me a lot.
edit :
I am using Unity 4, if any one wants to try my script with Older version of unity than you need to Import System.Xml and take care of other stuffs, otherwise script will generate lots of errors.