简体   繁体   English

获取属性值到XML文件

[英]Get attribute values to the XML-File

I got a issue with XML, and write some information on a XML-file. 我遇到了XML问题,并在XML文件上写了一些信息。
I got several xsd-files describing my XML. 我得到了几个描述我的XML的xsd文件。 I created one big .cs-file with "xsd/c testfile1.xsd testFile2.xsd..." etc. And everything went nice and looks good. 我用"xsd/c testfile1.xsd testFile2.xsd..."等创建了一个大的.cs文件。一切正常,看起来不错。
But if I take one created class ie testfile1.xsd, it looks like "<xs:complexType name="Header">" and inside that one there is this some ordinary xs:element and stuff, but also this: "<xs:attribute name="version" default="1.0.0.0"/>" . 但是,如果我使用一个创建的类,即testfile1.xsd,它看起来像"<xs:complexType name="Header">"并且在该类中有一些普通的xs:element和其他东西,但也有: "<xs:attribute name="version" default="1.0.0.0"/>" This is translated to: 这被翻译为:

"public Header() {
 this.versionField = "1.0.0.0";}" 

in the generated class 'Header'. 在生成的类“ Header”中。 And it got as well this field: private string versionField; 它也得到了这个字段: private string versionField;

. (There is of course a couple of other private fields as well, but those works good.). (当然也有其他两个私有领域,但是它们很好用。)。 So I create instances of all classes, fill them with data and write it as an XML-file with this: 因此,我创建了所有类的实例,并用数据填充它们,并使用以下代码将其编写为XML文件:

 - XmlSerializer XmlSerRoot = new XmlSerializer(typeof(RootInformation))
   (the root of my xml!) 
 - StringWriterWithEncoding strWriter = new StringWriterWithEncoding(Encoding.GetEncoding("iso-8859-1"));
 - XmlDocument documentInXML = new XmlDocument();
 - XmlSerRoot.Serialize(strWriter, rootInformation); (Here is the XML, filled with values, and the Header.version got the value 1.0.0.0) 
 - string XmlString;
 - XmlString = strWriter.ToString(); (Here I can look at the created xml when debugging in VS and now the version-information is gone)
 - documentInXML.LoadXml(XmlString);
 - documentInXML.Save(myPath);


But when I look at the xml-file this <Header> is not have anything like version. 但是,当我查看xml文件时,该<Header>没有类似的版本。 I want i to look like: <Header version="1.0.0.0"> 我希望我看起来像: <Header version="1.0.0.0">
I even tried to do it in code like: 我什至尝试用以下代码来做到这一点:

Header header = new Header(); 
header.version = header.version; 
and
with header.version = "1.0.0.0";

But still it's no version-text in the tag. 但是标记中仍然没有版本文本。 All other tags got their value. 所有其他标签都具有其价值。 Just this <Header> loose this extra information. 只是这个<Header>松散了这些额外的信息。
Does someone got a tip? 有人给小费吗? There is a lot of places I need this to work. 我在很多地方都需要这项工作。 Everything else is just working fine. 其他一切都很好。

Regards, /E 问候,/ E

Here is an 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 is the Root!";
            Header header = new Header();
            header.TestHeader = "This is 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
        {
            get { return this.headerField; }
            set { this.headerField = value; }
        }

        [System.Xml.Serialization.XmlElementAttribute(DataType = "normalizedString")]
        public string RootText
        {
            get { 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
            {
                get { return this.testHeaderField; }
                set { this.testHeaderField = value; }
            }

            [System.Xml.Serialization.XmlAttributeAttribute()]
            [System.ComponentModel.DefaultValueAttribute("1.0.0.0")]
            public string version
            {
                get { 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, I think I knov the issue. 没关系,我想我知道这个问题。 It's because this xsd.exe creates 'DefaultValueAttribute' for those fields. 这是因为此xsd.exe为这些字段创建了“ DefaultValueAttribute”。 That prevent this field to be in the xml. 这样可以防止该字段位于xml中。 Maybe some xsd-switch could have done that, I dunno... 也许有些xsd-switch可以做到这一点,我不知道...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM