Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取XML文件的属性值_C#_Xml_Xsd - Fatal编程技术网

C# 获取XML文件的属性值

C# 获取XML文件的属性值,c#,xml,xsd,C#,Xml,Xsd,我在XML方面遇到了一个问题,并在XML文件上编写了一些信息 我得到了几个描述XML的xsd文件。我用“xsd/c testfile1.xsd testFile2.xsd…”等创建了一个大的.cs文件,一切都很顺利,看起来也不错 但是如果我使用一个创建的类,即testfile1.xsd,它看起来像 ”其中有一些普通的xs:element和东西,还有这个:”。这被翻译成: "public Header() { this.versionField = "1.0.0.0";}" 在生成的类“He

我在XML方面遇到了一个问题,并在XML文件上编写了一些信息
我得到了几个描述XML的xsd文件。我用
“xsd/c testfile1.xsd testFile2.xsd…”
等创建了一个大的.cs文件,一切都很顺利,看起来也不错
但是如果我使用一个创建的类,即testfile1.xsd,它看起来像
其中有一些普通的xs:element和东西,还有这个:
。这被翻译成:

"public Header() {
 this.versionField = "1.0.0.0";}" 
在生成的类“Header”中。它也得到了这个字段:
private string versionField

。(当然也有一些其他的私人领域,但这些领域效果不错。)。因此,我创建所有类的实例,用数据填充它们,并用以下内容将其写入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);

但是当我查看xml文件时,这个
并没有类似的版本。我希望我看起来像:

我甚至试着在代码中这样做:

Header header = new Header(); 
header.version = header.version; 
and
with header.version = "1.0.0.0";
但标签中仍然没有版本文本。所有其他标记都得到了它们的值。仅此
即可释放此额外信息
有人得到小费吗?有很多地方我需要这个来工作。其他一切都很好

问候,/E

下面是一段示例代码:

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;}
            }
        }
    }
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml.Serialization;
使用System.IO;
使用System.Xml;
命名空间TestAppXML
{
班级计划
{
静态void Main(字符串[]参数)
{
RootInfo RootInfo=新的RootInfo();
rootInfo.RootText=“这是根!”;
页眉=新页眉();
header.TestHeader=“这是HeaderText!”;
rootInfo.Header=头;
XmlSerializer XmlSerRoot=新的XmlSerializer(typeof(RootInfo));
StringWriterWithEncoding strWriter=新的StringWriterWithEncoding(Encoding.GetEncoding(“iso-8859-1”);
XmlDocument documentInXML=新的XmlDocument();
Serialize(strWriter,rootInfo);
字符串XmlString;
XmlString=strWriter.ToString();
documentInXML.LoadXml(XmlString);
strWriter.Close();
documentInXML.Save(@“C:\TestXml.xml”);
}
}
/// 
[System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”,“4.0.30319.1”)]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true,命名空间=”http://acme.com")]
[System.Xml.Serialization.XmlRootAttribute(命名空间=”http://acme.com“,IsNullable=false)]
公共部分类RootInfo
{
专用标题字段;
私有字符串rootTextField;
公共标头
{
获取{返回this.headerField;}
设置{this.headerField=value;}
}
[System.Xml.Serialization.xmlementAttribute(DataType=“normalizedString”)]
公共字符串根文本
{
获取{返回this.rootTextField;}
设置{this.rootTextField=value;}
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”,“4.0.30319.1”)]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(命名空间=”http://acme.com")]
公共部分类头
{
私有字符串testHeaderField;
私有字符串版本字段;
公共标头()
{
this.versionField=“1.0.0.0”;
}
/// 
[System.Xml.Serialization.xmlementAttribute(DataType=“normalizedString”)]
公共字符串测试头
{
获取{返回this.testHeaderField;}
设置{this.testHeaderField=value;}
}
[System.Xml.Serialization.XmlAttributeAttribute()]
[System.ComponentModel.DefaultValueAttribute(“1.0.0.0”)]
公共字符串版本
{
获取{返回this.versionField;}
设置{this.versionField=value;}
}
}
类StringWriterWithEncoding:StringWriter
{
私有编码;
公共StringWriterWithEncoding(编码编码)
:base()
{MyEncoding=encoding;}
公共覆盖编码
{
获取{return MyEncoding;}
}
}
}

没关系,我想我知道这个问题。这是因为这个xsd.exe为这些字段创建了“DefaultValueAttribute”。防止此字段出现在xml中。也许某个xsd开关可以做到这一点,我不知道…

你能粘贴你正在使用的实际代码吗。好的,我举了一个小例子,其中这个版本的文本不在xml文件中。问候,/E