How I Update Value To Xml File? - c#

can i assign value to some element of xml file.my xml file format may be like this
<Object type="TextBox">
<Property name="Size">94, 20</Property>
<Property name="Name">TextBox1</Property>
<Property name="Location">70, 30</Property>
<Property name="TabIndex">1</Property>
<Property name="Text">This Is Text Box One.</Property>
</Object>
<Object type="TextBox">
<Property name="Size">94, 20</Property>
<Property name="Name">TextBox2</Property>
<Property name="Location">70, 70</Property>
<Property name="TabIndex">3</Property>
<Property name="Text">This Is Text Box Two.</Property>
</Object>
<Object type="Label">
<Property name="Size">94, 20</Property>
<Property name="Name">Label1</Property>
<Property name="Location">10, 110</Property>
<Property name="TabIndex">2</Property>
<Property name="Text">This Is Label One.</Property>
</Object>
<Object type="TextBox">
<Property name="Size">94, 20</Property>
<Property name="Name">TextBox3</Property>
<Property name="Location">70, 110</Property>
<Property name="TabIndex">4</Property>
<Property name="Text">This Is Text Box Three.</Property>
</Object>
and,i want to update text value of textbox2 to "This Is Update Text".
Is there any way to do like this?I use c#.net 2008.give me right way,please.
Regards
indi

The XmlDocument will do what you need:
http://support.microsoft.com/kb/301233

Just read the document in an XmlDocument after which you have to find the node, which is probably at a path somewhere similar to:
//Object[#type="textbox"][1]/Property[#name="Text"]
Just put that in an XmlNode object and update it's InnerText (I think).
I haven't tested it, but it should be something like this.

First you need to parse XML file using DOM parser, update the value using your DOM parser update function and then save/export XML back. You should let us know what language you use and we will give you links to most popular parsers.

There is a standard way of dealing with XML in almost any modern programming language; there is probably a one-line answer to your question in the documentation for your language. A 10-second Google search found me "Reading and Writing XML in C#"

On the Button click write this code
string sStartupPath = Application.StartupPath; //the path of ~/bin/debug/
XmlTextWriter objXmlTextWriter = new XmlTextWriter(sStartupPath + #"\selected.xml", null);
objXmlTextWriter.Formatting = Formatting.Indented;
objXmlTextWriter.WriteStartDocument();
objXmlTextWriter.WriteStartElement("TEST");
objXmlTextWriter.WriteStartElement("Name");
objXmlTextWriter.WriteStartAttribute("Surname"); //Attribute "Name"
objXmlTextWriter.WriteString("patel"); //Attribute Value
objXmlTextWriter.WriteEndAttribute();
objXmlTextWriter.WriteString(txtname.Text);
objXmlTextWriter.WriteEndElement(); //End of Name Element
objXmlTextWriter.WriteStartElement("Age") ;
objXmlTextWriter.WriteString(txtage.Text);
objXmlTextWriter.WriteEndElement(); //End of Age element
objXmlTextWriter.WriteEndElement();
objXmlTextWriter.Flush();
objXmlTextWriter.Close();
MessageBox.Show("The following file has been successfully created\r\n"
+ sStartupPath
+ #"\selected.xml", "XML", MessageBoxButtons.OK,
MessageBoxIcon.Information);
//It will create selected.xml file in to your project/bin/debug folder..
The out put of the xml file will be like this
value of txtName.text=ABC
value of txtAge.text=XYZ
<?xml version="1.0"?>
<TEST>
<Name Surname="patel">ABC</Name>
<Age>XYZ</Age>
</TEST>
Hope this will Help.

Related

NHibernate SchemaExport.Execute does not create table

Learning NHibernate by following this tutorial Your first NHibernate based application and I got to the point where you call
new SchemaExport(cfg).Execute(true, true, false);
in a test method to export the schema (create the Product table) for verifying NHibernate was set up correctly
[Test]
public void Can_generate_schema()
{
var cfg = new Configuration();
cfg.Configure();
cfg.AddAssembly(typeof(Product).Assembly);
new SchemaExport(cfg).Execute(true, true, false);
}
The mapping
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="MyFirstNHibernateProject" namespace="MyFirstNHibernateProject.Domain">
<class name="Product">
<id name="Id">
<generator class="guid"></generator>
</id>
<property name="Name"></property>
<property name="Category"></property>
<property name="Discontinued"></property>
</class>
</hibernate-mapping>
The configuration
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.MsSqlCeDialect</property>
<property name="connection.driver_class">NHibernate.Driver.SqlServerCeDriver</property>
<property name="connection.connection_string">Data Source=MyFirstNHibernateProject.sdf</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
The test passed and I can even see the output sql that create table but the table is not being created in the database (sql server compact), I refreshed the db in server explorer and it is still empty.
I have checked these posts
NHibernate SchemaExport not creating table
NHibernate SchemaExport does not create tables when “script” is false
NHibernate does not create Tables
but none of them solve my problem.
any ideas?
I would suggest to use full path in the connection string:
// instead of this
<property name="connection.connection_string"
>Data Source=MyFirstNHibernateProject.sdf</property>
// we should use
<property name="connection.connection_string"
>Data Source=C:\MyFirstNHibernateProject\MyFirstNHibernateProject.sdf</property>
Where the "C:\MyFirstNHibernateProject\" should be the full path to the "MyFirstNHibernateProject.sdf"
Also, in case you are suing CE 4.0 I would suggest to use this dialect:
<property name="dialect">NHibernate.Dialect.MsSqlCe40Dialect</property>

Want to find a specific node in xml

Currently, I have the xml as the following:
<Node_Parent>
<Column name="ColA" value="A" />
<Column name="ColB" value="B" />
<Column name="ColC" value="C" />
</Node_Parent>
How to get value B at ColB? I tried to use XmlDocument.SelectSingleNode("Node_Parent"), but I cannot access to ColB?
If I change to <ColB value="B" />, I can use XmlDocument.SelectSingleNode("Node_Parent/ColB").Attributes["value"].Value, but the xml format doesn't look good?
Thanks.
You need to write an XPath query in the SelectSingleNode:
var value = doc.SelectSingleNode(
"Node_Parent/Column[#name = 'ColB']"
).Attributes["value"].Value;
For more info on the XPath query language, see http://www.w3schools.com/xpath.
Good luck!

Nhibernate.Util causing ProxyFactoryFactoryNotConfiguredException when configuration line exists in config

We're building a project using NHibernate and Castle with the Validators project. I'm trying to upgrade it to the latest supported version between all of those. I've gotten the application working without errors, but I'm getting the exception below in a few of my unit tests. These are tests that don't actually touch the database in any way, but test functionality around the mapped entities.
NHibernate.Bytecode.ProxyFactoryFactoryNotConfiguredException:
The ProxyFactoryFactory was not configured.
Initialize 'proxyfactory.factory_class' property of the session-factory
configuration section with one of the available NHibernate.ByteCode providers.
Example:
<property name='proxyfactory.factory_class'>
NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu
</property>
Example:
<property name='proxyfactory.factory_class'>
NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle
</property>
[Continues down stack trace...]
Below is my config file:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory name="Linx2">
<property
name="connection.driver_class">NHibernate.Driver.NpgsqlDriver</property>
<property name="dialect">Linx2.Common.Framework.PostgreSQL83Dialect,
Linx2.Common.Framework</property>
<property name="connection.connection_string">[Hidden so I don't get fired.]</property>
<property name="adonet.batch_size">10</property>
<property name="show_sql">false</property>
<property name="use_outer_join">true</property>
<property name="command_timeout">60</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<property name="proxyfactory.factory_class">
NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle
</property>
<property name="connection.release_mode">after_transaction</property>
<mapping assembly="NHibernate.Collection.Observable" />
</session-factory>
</hibernate-configuration>
I have the config mapping there, and it works in the application. I'm also including the NHibernate.ByteCode dll. However, in these tests it is ignored. I've tried manually starting the configuration in individual test and even stopped and confirmed mid-test that the configuration has the item. However, the exception is thrown in the code below on the IsInitialized call.
if (NHibernateUtil.IsInitialized(ChildAssociations))
{
ChildAssociations.ForEach(x => isValid = isValid && x.Element.IsValid(validatedObjects));
}
This worked previously with no problems in NHibernate build for 2.2. Any help would be greatly appreciated. I've been beating my head on it for the last 4 hours.
Apparently NHibernateUtil needs not only to have the configuration initialized, but needs the session factory to be built. I was able to get it to work by manually running the config and building the session factory in the tests. It wasn't a problem in the app because the session factory had been built before hand.
var cfg = new NHibernate.Cfg.Configuration().Configure();
var sessionFactory = cfg.BuildSessionFactory();

error while compiling

error on line : config.AddAssembly("Hibernatetest");
Error Message : Could not compile the mapping document: Hibernatetest.Company.hbm.xml
Configuration config = new Configuration();
ISessionFactory factory;
config.SetProperty(NHibernate.Cfg.Environment.ConnectionProvider, "NHibernate.Connection.DriverConnectionProvider");
config.SetProperty(NHibernate.Cfg.Environment.Dialect, "NHibernate.Dialect.MsSql2008Dialect");
config.SetProperty(NHibernate.Cfg.Environment.ConnectionDriver , "NHibernate.Driver.SqlClientDriver");
config.SetProperty(NHibernate.Cfg.Environment.ConnectionString, "Data Source=(local);Initial Catalog=Chitty;Integrated Security=True");
config.SetProperty(NHibernate.Cfg.Environment.ProxyFactoryFactoryClass, "NHibernate.ByteCode.Castle.Core.ProxyFactoryFactory ,NHibernate.ByteCode.Castle.Core");
config.AddAssembly("Hibernatetest");
this is the company xml file
<hibernate-mapping xmlns="urn:nhibernate-mapping-3.0" namespace="Hibernatetest"
assembly="Hibernatetest">
<class name="Company" table="Company">
<id name="company_Id">
<generator class="identity" />
</id>
<property name="company_name" type="string" />
<property name="company_email" type="string"/>
<property name="company_size" type="string"/>
</class>
</hibernate-mapping>
please help
Hi
I don't know why you put your namespace in the hbm file:
Hibernatetest.Company.hbm.xml
and also put the namespace in the class name:
This is how should look:
Company.hbm.xml
and
<hibernate-mapping xmlns="urn:nhibernate-mapping-3.0" namespace="Hibernatetest"
assembly="Hibernatetest">
<class name="Company" table="Company">
<id name="company_Id">
<generator class="identity" />
</id>
<property name="company_name" type="string" />
<property name="company_email" type="string"/>
<property name="company_size" type="string"/>
</class>
</hibernate-mapping>
Also you can get rid off the type but it's a good practice to put the column attribute in a property.
The class name is wrong, write the class name with the full namespace ( if you don't want to specify the namespace at the mapping level. The offending part is for sure the comma after the Company. And btw, if it does not solve, please add the class too in your question: it looks strange you have defined a c# class with properties named "company_name".

NHibernate Duplicate class/entity mapping problem

I've have started my foray into C#.NET and NHibernate and I'm finally stuck on an exception I can't seem to figure out, and Google isn't helping.
I'm getting a "NHibernate.DuplicateMappingException : Duplicate class/entity mapping" on my Parent class. Below is my mapping file for the Parent class, and the Youth class that uses the Parent class:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Surrix.Cerberus.YouthData"
namespace="Surrix.Cerberus.YouthData.Domain">
<class name="Parent">
<id name="parentId">
<generator class="guid" />
</id>
<property name="firstName" not-null="true" />
<property name="lastName" not-null="true" />
<property name="homePhone" />
<property name="parentEmail" />
<property name="relationshipToYouth" />
<!-- Address component that should map to the Address class -->
<component name="parentAddress">
<property name="street" />
<property name="state" />
<property name="zipCode" />
<property name="city" />
</component>
</class>
</hibernate-mapping>
And here is the relevant part of the Youth class (it is considerably bigger)
<set name="YouthParents" table="YouthParents" cascade="none">
<key column="youthId" />
<many-to-many column="parentId" class="Parent"/>
</set>
Only other thing is the Youth class also has the firstName and lastName properties, but I can't see that being a problem.
Make sure you are not doing both of these 2 things.
(1) adding the assembly in code:
// Code Configuration
var cfg = new Configuration();
cfg.Configure();
cfg.AddAssembly(typeof(Employee).Assembly);
// Presuming Employee resides in "MyAssembly" as seen below.
(2) And then also adding the assembly in the config file:
<!-- .config configuration -->
<session-factory>
<!-- bunch of other stuff -->
<mapping assembly="MyAssembly"/> <!-- as in MyAssembly.dll -->
</session-factory>
You are adding the file or assembly containing the mapping twice to your Configuration object.
I had this problem, and solved it by putting this statement in hibernate.cfg.xml file:
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
Another possible cause to generate this error is multiple hbm files referencing the same Assembly during a Configuration.AddAssembly.
All hbm files in the same assembly are processed with one AddAssembly call.
As it gives a duplicate class entity mapping I can only imagen that you have two or more *.xml.hbm files referring to the same .net class.
Make sure that the xml class element for your Youth class does not have the same value for the name attribute.

Categories