如何添加C#xml序列化的属性
我在序列化和对象方面存在问题,我可以得到它来创建所有正确的输出,除了我有一个需要值和属性的元素。这是所需的输出:
<Root>
<Method>Retrieve</Method>
<Options>
<Filter>
<Times>
<TimeFrom>2009-06-17</TimeFrom>
</Times>
<Document type="word">document name</Document>
</Filter>
</Options>
</AdCourierAPI>
我可以构建所有内容,但找不到设置文档类型属性的方法,这是对象类的一部分,
[XmlRoot("Root"), Serializable]
public class Root
{
[XmlElement("Method")]
public string method="RetrieveApplications";
[XmlElement("Options")]
public _Options Options;
}
public class _Options
{
[XmlElement("Filter")]
public _Filter Filter;
}
public class _Filter
{
[XmlElement("Times")]
public _Times Times;
[XmlElement("Documents")]
public string Documents;
}
它给了我:
<Document>document name</Document>
而不是:
<Document type="word">document name</Document>
但是我找不到方法来纠正此问题, 请指教。
谢谢
I am having an issue with serializing and object, I can get it to create all the correct outputs except for where i have an Element that needs a value and an attribute. Here is the required output:
<Root>
<Method>Retrieve</Method>
<Options>
<Filter>
<Times>
<TimeFrom>2009-06-17</TimeFrom>
</Times>
<Document type="word">document name</Document>
</Filter>
</Options>
</AdCourierAPI>
I can build all of it but can not find a way to set the Document type attribute, here is a segment of the object class
[XmlRoot("Root"), Serializable]
public class Root
{
[XmlElement("Method")]
public string method="RetrieveApplications";
[XmlElement("Options")]
public _Options Options;
}
public class _Options
{
[XmlElement("Filter")]
public _Filter Filter;
}
public class _Filter
{
[XmlElement("Times")]
public _Times Times;
[XmlElement("Documents")]
public string Documents;
}
which gives me:
<Document>document name</Document>
rather than:
<Document type="word">document name</Document>
but I can not find a way to correct this, please advise.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您在哪里存储了
类型
?通常您可能会有类似的东西:
Where do you have the
type
stored?Normally you could have something like:
字符串
类没有类型
属性,因此您无法使用它来创建所需的输出。您应该创建一个文档
类:您应该将
文档
属性更改为typedocument
The
string
class doesn't have atype
property, so you can't use it to create the desired output. You should create aDocument
class instead :And you should change the
Document
property to typeDocument
听起来您需要一个额外的类:
其中一个实例(在示例中)将具有
type =“ Word”
和name =“ document name”
;文档
将是list&lt; document&gt;
。顺便说一句 - 公共领域很少是一个好主意...
It sounds like you need an extra class:
Where an instance (in the example) would have
Type = "word"
andName = "document name"
;documents
would be aList<Document>
.By the way - public fields are rarely a good idea...
您可以使用XMLWriter而代替XMLSerialization来获得此效果。
它更为复杂,但是如果您在模型中有很多字符串,则将是更干净的解决方案。
例如,创建您自己的自定义图表:
然后在模型中添加它,这样:
最后一部分是使用此参数创建XML。
您可以这样做:
在myxml中应该是这样的:
(值是示例)
当然,您可以以其他方式进行。
在这里,您有一些对我有帮助的文档:
https://学习。 microsoft.com/en-us/dotnet/api/system.xml.xmlwriter?view=netframework-4.8#writing_elements
You can use XmlWriter instead XmlSerialization to get this effect.
It is more complex but if you have a lot of strings in model it will be cleaner solution.
Create your own CustomeAttribute, for example:
Then in model add it, like that:
The last part is to create xml with this arguments.
You can do it likes that:
In myXml should be something like that:
(values are examples)
You can do it in other way, of course.
Here you have some docs which helped me:
https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmlwriter?view=netframework-4.8#writing_elements