[英]XML namespace in ASP.net MVC, C#
I'm trying to get an XML file generated using a namespace as such: 我正在尝试使用这样的名称空间来生成XML文件:
<namespace:Example1>
<namespace:Part1>Value1</namespace:Part1>
</namespace:Example1>
I've tried using 我试过使用
[XmlAttribute(Namespace = "namespace")]
public string Namespace { get; set; }
but I'm clearly missing something. 但我显然缺少了一些东西。 The structure I've used is
我使用的结构是
[XmlRoot("Example1")]
public class Blah
{
[XmlAttribute(Namespace = "namespace")]
public string Namespace { get; set; }
but all I get is 但我得到的只是
<Example1>
<Part1>Value1</Part1>
</Example1>
Any help would be greatly appreciated. 任何帮助将不胜感激。
Edit: 编辑:
[XmlRoot(ElementName="Chart2", Namespace="vc")]
doesn't work. 不起作用。
You can use the XmlSerializerNamespaces class to add the prefix for a given namespace in the xml. 您可以使用XmlSerializerNamespaces类在xml中添加给定名称空间的前缀。
I hope the below code will he you better. 希望以下代码对您有帮助。
[XmlRoot(ElementName = "Example1")]
public class Blah
{
public string Part1 { get; set; }
}
Blah bl = new Blah();
bl.Part1 = "MyPart1";
// Serialization
/* Create an XmlSerializerNamespaces object and add two prefix-namespace pairs. */
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("namespace", "test");
XmlSerializer s = new XmlSerializer(typeof(Blah),"test");
TextWriter w = new StreamWriter(@"c:\list.xml");
s.Serialize(w, bl,ns);
w.Close();
/* Output */
<?xml version="1.0" encoding="utf-8"?>
<namespace:Example1 xmlns:namespace="test">
<namespace:Part1>MyPart1</namespace:Part1>
</namespace:Example1>
Can you try this on your Model.cs: 您可以在Model.cs上尝试一下吗:
Copy the whole XML, then on the Model.cs: Edit > Paste Special > Paste XML as Classes. 复制整个XML,然后在Model.cs上:编辑>选择性粘贴>将XML粘贴为类。
Might help you. 可能会帮助您。 ;)
;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.