c# - Get attribute values to the XML-File -
i got issue xml, , write information on xml-file.
got several xsd-files describing xml. created 1 big .cs-file "xsd/c testfile1.xsd testfile2.xsd..."
etc. , went nice , looks good.
if take 1 created class i.e. testfile1.xsd, looks "<xs:complextype name="header">"
, inside 1 there ordinary xs:element , stuff, this: "<xs:attribute name="version" default="1.0.0.0"/>"
. translated to:
"public header() { this.versionfield = "1.0.0.0";}"
in generated class 'header'. , got field: private string versionfield;
. (there of course couple of other private fields well, works good.). create instances of classes, fill them data , write xml-file this:
- xmlserializer xmlserroot = new xmlserializer(typeof(rootinformation)) (the root of xml!) - stringwriterwithencoding strwriter = new stringwriterwithencoding(encoding.getencoding("iso-8859-1")); - xmldocument documentinxml = new xmldocument(); - xmlserroot.serialize(strwriter, rootinformation); (here xml, filled values, , header.version got value 1.0.0.0) - string xmlstring; - xmlstring = strwriter.tostring(); (here can @ created xml when debugging in vs , version-information gone) - documentinxml.loadxml(xmlstring); - documentinxml.save(mypath);
but when @ xml-file <header>
not have version. want like: <header version="1.0.0.0">
tried in code like:
header header = new header(); header.version = header.version; , header.version = "1.0.0.0";
but still it's no version-text in tag. other tags got value. <header>
loose information.
got tip? there lot of places need work. else working fine.
regards, /e
here piece of example-code:
using system; using system.collections.generic; using system.linq; using system.text; using system.xml.serialization; using system.io; using system.xml; namespace testappxml { class program { static void main(string[] args) { rootinfo rootinfo = new rootinfo(); rootinfo.roottext = "this root!"; header header = new header(); header.testheader = "this headertext!"; rootinfo.header = header; xmlserializer xmlserroot = new xmlserializer(typeof(rootinfo)); stringwriterwithencoding strwriter = new stringwriterwithencoding(encoding.getencoding("iso-8859-1")); xmldocument documentinxml = new xmldocument(); xmlserroot.serialize(strwriter, rootinfo); string xmlstring; xmlstring = strwriter.tostring(); documentinxml.loadxml(xmlstring); strwriter.close(); documentinxml.save(@"c:\testxml.xml"); } } /// <remarks/> [system.codedom.compiler.generatedcodeattribute("xsd", "4.0.30319.1")] [system.serializableattribute()] [system.diagnostics.debuggerstepthroughattribute()] [system.componentmodel.designercategoryattribute("code")] [system.xml.serialization.xmltypeattribute(anonymoustype = true, namespace = "http://acme.com")] [system.xml.serialization.xmlrootattribute(namespace = "http://acme.com", isnullable = false)] public partial class rootinfo { private header headerfield; private string roottextfield; public header header { { return this.headerfield; } set { this.headerfield = value; } } [system.xml.serialization.xmlelementattribute(datatype = "normalizedstring")] public string roottext { { return this.roottextfield; } set { this.roottextfield = value; } } } [system.codedom.compiler.generatedcodeattribute("xsd", "4.0.30319.1")] [system.serializableattribute()] [system.diagnostics.debuggerstepthroughattribute()] [system.componentmodel.designercategoryattribute("code")] [system.xml.serialization.xmltypeattribute(namespace = "http://acme.com")] public partial class header { private string testheaderfield; private string versionfield; public header() { this.versionfield = "1.0.0.0"; } /// <remarks/> [system.xml.serialization.xmlelementattribute(datatype = "normalizedstring")] public string testheader { { return this.testheaderfield; } set { this.testheaderfield = value; } } [system.xml.serialization.xmlattributeattribute()] [system.componentmodel.defaultvalueattribute("1.0.0.0")] public string version { { return this.versionfield; } set { this.versionfield = value; } } } class stringwriterwithencoding : stringwriter { private encoding myencoding; public stringwriterwithencoding(encoding encoding) : base() {myencoding = encoding;} public override encoding encoding { get{return myencoding;} } } }
nevermind, think knov issue. it's because xsd.exe creates 'defaultvalueattribute' fields. prevent field in xml. maybe xsd-switch have done that, dunno...
Comments
Post a Comment