אפשרי לטעון/לשמור מידע בקבצי טקסט ב#VISUAL STUDIO - C? - תכנות - HWzone פורומים
עבור לתוכן
  • צור חשבון

אפשרי לטעון/לשמור מידע בקבצי טקסט ב#VISUAL STUDIO - C?


IlanF

Recommended Posts

טוב.. תודה. אני יראה אם זה יעזור לי.. :xyxthumbs:

(אם אתה תוהה מה אני עושה ולמה כל כך הרבה שאלות.. http://stage6.divx.com/SA-MP-FXP-server/blog/ )

קראתי עכשיו קצת על זה ו...

XmlTextReader provides forward-only, read-only access to a stream of XML data.

אני רוצה גם לשמור בו מידע...

קישור לתוכן
שתף באתרים אחרים

string myPath = @"c:\bla.xml";
XmlTextWriter textWriter = new XmlTextWriter(myPath, null);
textWriter.WriteStartDocument();

textWriter.WriteStartElement("User");
textWriter.WriteStartElement("Name", "");
textWriter.WriteString("Pure-Gold");
textWriter.WriteEndElement();

textWriter.WriteStartElement("Age", "");
textWriter.WriteString("23");
textWriter.WriteEndElement();
textWriter.WriteEndElement();
textWriter.Close();

מה שאתה מקבל ממה שרשום פה זה:

<User>
<Name>Pure-Gold</Name>
<Age>23</Age>
</User>

קישור לתוכן
שתף באתרים אחרים

תכניס בפרוייקט שלך

using system.xml;


XmlDocument xm = new XmlDocument();// creates new document
XmlDeclaration = xm.CreateXmlDeclaration("1.0",null, null);// create decliration
xm.AppendChild();// appends decliration to document
XmlElement rootElement = xm.CreateElement("Settings");// new root element
XmlElement visualSettings = xm.CreateElement("Visual Settings");// new element inside root,
XmlAttribute xa = xm.CreateAttribute("divx support");
xa.Value = "true";
visualSettings.Attributes.Append(xa);
XmlElement graphics = xm.CreateElement("Graphics");
graphics.InnerText = "this is just to show you";// inner text of an element
visualSettings.AppendChild(graphics);
rootElement.AppendChild(visualSettings);//appends to root element
xm.AppendChild(rootElement);// appends root element
xm.Save(@"c:\test.xml");// saves document

////////////////////////////////////////////////////
XmlDocument test = new XmlDocument();
test.Load(@"c:\test.xml");
XmlNodeList nodelist = test.GetElementsByTagName("Settings");
foreach (XmlNode xn in nodelist)
{
if (xn.NodeType == XmlNodeType.Element)
DoWhatever();
}
}




קישור לתוכן
שתף באתרים אחרים

string myPath = @"c:\bla.xml";
XmlTextWriter textWriter = new XmlTextWriter(myPath, null);
textWriter.WriteStartDocument();

textWriter.WriteStartElement("User");
textWriter.WriteStartElement("Name", "");
textWriter.WriteString("Pure-Gold");
textWriter.WriteEndElement();

textWriter.WriteStartElement("Age", "");
textWriter.WriteString("23");
textWriter.WriteEndElement();
textWriter.WriteEndElement();
textWriter.Close();

מה שאתה מקבל ממה שרשום פה זה:

<User>
<Name>Pure-Gold</Name>
<Age>23</Age>
</User>

עובד, יוצר לי קובץ XML.. עכשיו איך אני יכול לבדוק מה רשום לי נגיד בין <Name></Name> ?

ואיך אני עורך אותו?

קישור לתוכן
שתף באתרים אחרים

שתי דרכים:

1. לקרוא את כל ה-XML לתוך אובייקט XmlDocument, ככה:

XmlDocument doc = new XmlDocument();
doc.Load(@"c:\bla.xml");

ועכשיו אתה יכול לגשת לכל ה-nodes שב-XML באמצעות doc.ChildNodes, או לחילופין לגשת באמצעות XPath.

2. לקרוא באמצעות XmlReader, כמו בדוגמה הזו:

http://msdn2.microsoft.com/en-us/library/t9bfea29(VS.80).aspx

קישור לתוכן
שתף באתרים אחרים

אני לא ממש מבין.. אני מתחיל להסתבך...

אז ככה.. יש לי קובץ XML כזה:

settings.xml

<?xml version="1.0"?>
<Settings>
<Mode>mode1</Mode>
</Settings>

אני רוצה לקרוא את התג <Mode> ולבדוק אם יש שם mode1 או mode2.

אם יש mode1, אני רוצה שיציג הודעת שגיאה (אני ישנה אחר כך למה שאני צריך) שתכתוב mode1. ואם יש שם mode2 יציג הודעה mode2.

וגם אני רוצה שכל פעם שיש mode1 או mode2, התוכנה תחליף ל mode השני (הכוונה אם היה רשום mode1, התוכנה תשנה את זה ל-mode2 ולהפך)

קישור לתוכן
שתף באתרים אחרים

אני מקבל שגיאה כשאני מנסה להריץ את זה...

untitledyk8.jpg

See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.Xml.XmlException: The ' ' character, hexadecimal value 0x20, cannot be included in a name.
at System.Xml.XmlDocument.CheckName(String name)
at System.Xml.XmlElement..ctor(XmlName name, Boolean empty, XmlDocument doc)
at System.Xml.XmlDocument.CreateElement(String prefix, String localName, String namespaceURI)
at System.Xml.XmlDocument.CreateElement(String name)
at Mod_Replacer.Form1.replase_btn_Click(Object sender, EventArgs e) in C:\Documents and Settings\Ilan\My Documents\Visual Studio 2005\Projects\Mod-Replacer\Mod-Replacer\Form1.cs:line 75
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.1378 (REDBITSB2.050727-1300)
CodeBase: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
Mod-Replacer
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Documents%20and%20Settings/Ilan/My%20Documents/Visual%20Studio%202005/Projects/Mod-Replacer/Mod-Replacer/bin/Debug/Mod-Replacer.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.1378 (REDBITSB2.050727-1300)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.1378 (REDBITSB2.050727-1300)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.1378 (REDBITSB2.050727-1300)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Configuration
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.1378 (REDBITSB2.050727-1300)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.1378 (REDBITSB2.050727-1300)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.

קישור לתוכן
שתף באתרים אחרים

אני לא ממש מבין.. אני מתחיל להסתבך...

אז ככה.. יש לי קובץ XML כזה:

settings.xml

<?xml version="1.0"?>
<Settings>
<Mode>mode1</Mode>
</Settings>

אני רוצה לקרוא את התג <Mode> ולבדוק אם יש שם mode1 או mode2.

אם יש mode1, אני רוצה שיציג הודעת שגיאה (אני ישנה אחר כך למה שאני צריך) שתכתוב mode1. ואם יש שם mode2 יציג הודעה mode2.

וגם אני רוצה שכל פעם שיש mode1 או mode2, התוכנה תחליף ל mode השני (הכוונה אם היה רשום mode1, התוכנה תשנה את זה ל-mode2 ולהפך)

בדוגמא הזאת אני בודק מה יש בתג mode ומשנה אותו... מקווה שתבין.


XmlDocument doc = new XmlDocument();
doc.Load(@"c:\settings.xml");
XmlNodeList xnl = doc.GetElementsByTagName("Mode");
switch (xnl[0].InnerText)
{
case "mode1":
xnl[0].InnerText = "mode2";
MessageBox.Show("mode changed");
break;
case "mode2":
xnl[0].InnerText = "mode1";
default:
break;
}
doc.Save(@"c:\settings.xml");


קישור לתוכן
שתף באתרים אחרים

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

×
  • צור חדש...