我需要从c#代码生成这个xml但是我在xmlwriter中丢失了提供多个编写器。任何拥有xml专业知识的人都可以帮助我编写C#代码来生成xml下面的代码:
<?xml version="1.0" encoding="utf-16" ?>
<Request xmlns="http://www.ABC.com/submit" xmlns:l="http://www.ABC.com/link" l:source="SRC" l:date="2014-03-05">
<MyData l:identifier="PRAV" l:value="151.19448366182007269092408546">
<DATA1 l:value="0.151328110447635" />
<DATA2 l:value="0.6461191930062688087600920641" />
<DATA3 l:value="0.144793773777417" />
</MyData>
</Request>
答案 0 :(得分:1)
使用Visual Studio的Paste Special功能。
通过这种方式,您可以复制XML结构(前提是它当前不是有效的。)
选择
这将为您提供一个表示XML的类:
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.ABC.com/submit")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.ABC.com/submit", IsNullable = false)]
public partial class Request
{
private RequestMyData myDataField;
private string sourceField;
private System.DateTime dateField;
/// <remarks/>
public RequestMyData MyData
{
get
{
return this.myDataField;
}
set
{
this.myDataField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://www.ABC.com/link")]
public string source
{
get
{
return this.sourceField;
}
set
{
this.sourceField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://www.ABC.com/link", DataType = "date")]
public System.DateTime date
{
get
{
return this.dateField;
}
set
{
this.dateField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.ABC.com/submit")]
public partial class RequestMyData
{
private RequestMyDataDATA1 dATA1Field;
private RequestMyDataDATA2 dATA2Field;
private RequestMyDataDATA3 dATA3Field;
private string identifierField;
private decimal valueField;
/// <remarks/>
public RequestMyDataDATA1 DATA1
{
get
{
return this.dATA1Field;
}
set
{
this.dATA1Field = value;
}
}
/// <remarks/>
public RequestMyDataDATA2 DATA2
{
get
{
return this.dATA2Field;
}
set
{
this.dATA2Field = value;
}
}
/// <remarks/>
public RequestMyDataDATA3 DATA3
{
get
{
return this.dATA3Field;
}
set
{
this.dATA3Field = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://www.ABC.com/link")]
public string identifier
{
get
{
return this.identifierField;
}
set
{
this.identifierField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://www.ABC.com/link")]
public decimal value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.ABC.com/submit")]
public partial class RequestMyDataDATA1
{
private decimal valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://www.ABC.com/link")]
public decimal value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.ABC.com/submit")]
public partial class RequestMyDataDATA2
{
private decimal valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://www.ABC.com/link")]
public decimal value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.ABC.com/submit")]
public partial class RequestMyDataDATA3
{
private decimal valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://www.ABC.com/link")]
public decimal value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
答案 1 :(得分:1)
如果您将类编写为DataContracts并将XML属性分配给DataMembers,那么您应该能够获得所需的内容。
[DataContract]
public class Request
{
[DataMember, XmlAttribute]
public string Source { get; set; }
[DataMember]
public MyData MyData { get; set; }
}
[DataContract]
public class MyData
{
[DataMember, XmlAttribute]
public string Identifier { get; set; }
[DataMember, XmlAttribute]
public double Value { get; set; }
[DataMember]
public List<Data> Datum { get; set; }
}
[DataContract]
public class Data
{
[DataMember, XmlAttribute]
public double Value { get; set; }
}
并测试
var listOfData = new List<Data> {new Data {Value = 0.15}, new Data {Value = 0.64}};
var myData = new MyData {Identifier = "PRAV", Value = 151.194, Datum = listOfData};
var request = new Request {MyData = myData, Source = "SRC"};
XmlSerializer ser = new XmlSerializer(typeof(Request));
StringWriter sw = new StringWriter();
ser.Serialize(sw, request);
Console.WriteLine(sw.ToString());
Thiis将导致
<?xml version="1.0" encoding="utf-16"?>
<Request
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http:/ /www.w3.org/2001/XMLSchema" Source="SRC">
<MyData Identifier="PRAV" Value="151.194">
<Datum>
<Data Value="0.15" />
<Data Value="0.64" />
</Datum>
</MyData>
</Request>
答案 2 :(得分:1)
您可以使用XDocument
从C#创建XML文件:
using System.Xml.Linq;
var doc = new XDocument(new XDeclaration("1.0", "utf-16", ""));
XNamespace ns = "http://www.ABC.com/submit";
XNamespace l = "http://www.ABC.com/link";
//<Request>
doc.Add(new XElement(ns+"Request",
//xmlns="http://www.ABC.com/submit"
new XAttribute("xmlns", "http://www.ABC.com/submit"),
//xmlns:l="http://www.ABC.com/link"
new XAttribute(XNamespace.Xmlns + "l", "http://www.ABC.com/link"),
//l:source="SRC"
new XAttribute(l+"source", "SRC"),
//l:date="2014-03-05"
new XAttribute(l + "date", "2014-03-05"),
//<MyData>
new XElement(ns+"MyData",
//l:identifier="PRAV"
new XAttribute(l+"identifier", "PRAV"),
//l:value="151.19448366182007269092408546"
new XAttribute(l + "value", "151.19448366182007269092408546"),
//<DATA1>
new XElement(ns+"DATA1",
//l:value="0.151328110447635"
new XAttribute(l + "value", "0.151328110447635")),
//<DATA2>
new XElement(ns+"DATA2",
//l:value="0.6461191930062688087600920641"
new XAttribute(l + "value", "0.6461191930062688087600920641")),
//<DATA3>
new XElement(ns+"DATA3",
//l:value="0.144793773777417"
new XAttribute(l + "value", "0.144793773777417")))));
doc.Save("xml_name.xml");