获取XML文件的属性值 [英] Get attribute values to the XML-File
问题描述
我遇到了XML问题,并在XML文件上写了一些信息。
我有几个描述我的XML的xsd文件。我用
xsd / c testfile1.xsd testFile2创建了一个大的.cs文件。 xsd ......
等等。一切都很顺利,看起来不错。
但是如果我使用一个创建的类,即testfile1.xsd,它看起来像< xs:complextype name =Headerxmlns:xs =#unknown>在那一个里面有一些普通的xs:元素和东西,但是这个:< xs:attribute name =versiondefault =1.0.0.0>。这被翻译为:
public Header(){
this.versionField =1.0.0.0;}
生成的类''标题''。它也得到了这个字段:private string versionField;
。 (当然还有其他几个私人领域,但这些领域都很好。)所以我创建了所有类的实例,用数据填充它们并将其写成XML文件:
但是当我查看xml文件时这个< ;页眉和GT;没有版本的东西。我希望我看起来像:< Header version =1.0.0.0>
这是一段示例代码:
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Xml.Serialization;
使用 System.IO;
使用 System.Xml;
命名空间 TestAppXML
{
class 程序
{
静态 void Main( string [] args)
{
RootInfo rootInfo = new RootInfo();
rootInfo.RootText = 这是Root!;
标头标头= 新标头();
header.TestHeader = 这是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:\ TestMml.xml);
}
}
/// < 备注/ >
[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 标头headerField;
private string rootTextField;
public 标题标题
{
get {返回 此 .headerField; }
set { this .headerField = value 跨度>; }
}
[System.Xml.Serialization.XmlElementAttribute(DataType = normalizedString)]
public string RootText
{
获取 {返回 此 .rootTextField ; }
set { this .rootTextField = value 跨度>; }
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute( xsd, 4.0.30319.1)]
[ System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute( 代码)]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = http://acme.com)]
public partial class 标题
{
私有 string testHeaderField;
private string versionField;
public 标题()
{
此 .versionField = < span class =code-string> 1.0.0.0;
}
/// < 备注/ >
[System.Xml .Serialization.XmlElementAttribute(DataType = normalizedString)]
public string TestHeader
{
get { return 此 .testHeaderField; }
set { this .testHeaderField = value 跨度>; }
}
[System.Xml.Serialization.XmlAttributeAttribute()]
[System.ComponentModel.DefaultValueAttribute( 1.0.0.0)]
public 字符串版本
{
获取 {返回 此跨度> .versionField; }
set { this .versionField = value 跨度>; }
}
}
class StringWriterWithEncoding:StringWriter
{
私人编码MyEncoding;
public StringWriterWithEncoding(编码编码)
: base ()
{MyEncoding = encoding;}
public 覆盖编码编码
{
获取 {返回 MyEncoding;}
}
}
}
我甚至尝试用以下代码来做:
< pre lang =c#>标题标题= new 标题();
header.version = header.version;
和
,header.version = 1.0.0.0;
但是标签中还没有版本文本。所有其他标签都有其价值。只是这个< Header>松散这些额外的信息。
有人得到了小费吗?有很多地方我需要这个工作。其他一切都运转正常。
问候,
/ E
< blockquote>没关系,我想我知道这个问题。这是因为这个xsd.exe为这些字段创建了''DefaultValueAttribute''。这会阻止此字段位于xml中。也许有些xsd-switch可以做到这一点,我不知道......
I got a issue with XML, and write some information on a XML-file.
I got several xsd-files describing my XML. I created one big .cs-file with
"xsd/c testfile1.xsd testFile2.xsd..."
etc. And everything went nice and looks good.
But if I take one created class i.e. testfile1.xsd, it looks like "<xs:complextype name="Header" xmlns:xs="#unknown">" 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">". This is translated to:
"public Header() {
this.versionField = "1.0.0.0";}"
in the generated class ''Header''. And it got as well this field: 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:
But when I look at the xml-file this <Header> is not have anything like version. I want i to look like: <Header version="1.0.0.0">
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;}
}
}
}
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.
Does someone got a tip? There is a lot of places I need this to work. Everything else is just working fine.
Regards,
/E
Nevermind, I think I knov the issue. It''s because this xsd.exe creates ''DefaultValueAttribute'' for those fields. That prevent this field to be in the xml. Maybe some xsd-switch could have done that, I dunno...
这篇关于获取XML文件的属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!