获取xml文件的属性值
本文关键字:属性 文件 xml 获取 | 更新日期: 2023-09-27 18:07:49
我遇到了XML的问题,并在XML文件中写入了一些信息。
我得到了几个描述我的XML的xsd文件。我用"xsd/c testfile1.xsd testFile2.xsd..."
等创建了一个大的。cs文件。一切都很顺利,看起来也很好。
但如果我创建一个类,即testfile1。Xsd,看起来像"<xs:complexType name="Header">"
里面有一些普通的x元素,还有"<xs:attribute name="version" default="1.0.0.0"/>"
。这被翻译成:
"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 version="1.0.0.0">
我甚至尝试在代码中这样做:
Header header = new Header();
header.version = header.version;
and
with header.version = "1.0.0.0";
但是标签中仍然没有版本文本。所有其他标签都有自己的价值。只是这个<Header>
丢失了这个额外的信息。
有人有小费吗?我需要在很多地方用上它。其他一切都很好。
问候,/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;}
}
}
}
没关系,我想我知道这个问题。这是因为这个xsd.exe为这些字段创建了'DefaultValueAttribute'。防止该字段出现在xml中。也许一些xsd-switch可以做到这一点,我不知道…