C#xmlwriter sql server
从SQL Server 2008开始,使用xml自动、根('root')、类型、元素xsini的C#xmlwriter sql server,c#,xml,nodes,C#,Xml,Nodes,从SQL Server 2008开始,使用xml自动、根('root')、类型、元素xsini的来获取此输出: <ROOT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <node> <nodeid>28</nodeid> <account_no>0</account_no> <address1>15 CANCUN CT</addr
来获取此输出:
<ROOT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<node>
<nodeid>28</nodeid>
<account_no>0</account_no>
<address1>15 CANCUN CT</address1>
<serial_no>112199543</serial_no>
<x_lat>25.95513358000</x_lat>
<y_lon>-97.49027147000</y_lon>
<alarm>
<alarmid>Outage</alarmid>
<alarmtime>2012-08-03T16:27:15.830</alarmtime>
</alarm>
<alarm>
<alarmid>Restore</alarmid>
<alarmtime>2012-08-06T09:41:53.313</alarmtime>
</alarm>
</node>
</ROOT>
这段代码的输出是这个(两个节点元素),我只希望它和格式化的SQLServer输出一样
<ROOT xmlns="http://www.w3.org/2001/XMLSchema-instance">
<node>
<nodeid>28</nodeid>
<account_no>0</account_no>
<address1>15 CANCUN CT</address1>
<serial_no>112199543</serial_no>
<alarmid>Outage</alarmid>
<alarmtime>8/3/2012 4:27:15 PM</alarmtime>
<x_lat>25.95513358000</x_lat>
<y_lon>-97.49027147000</y_lon>
</node>
<node>
<nodeid>28</nodeid>
<account_no>0</account_no>
<address1>15 CANCUN CT</address1>
<serial_no>112199543</serial_no>
<alarmid>Restore</alarmid>
<alarmtime>8/6/2012 9:41:53 AM</alarmtime>
<x_lat>25.95513358000</x_lat>
<y_lon>-97.49027147000</y_lon>
</node>
</ROOT>
我发现序列化对象比在代码中构建要容易得多
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Xml.Linq;
namespace SO.XmlSqlServer
{
class Program
{
static void Main(string[] args)
{
var node = new SqlOutput
{
NodeId = 28,
AccountNumber = 0,
AddressLine1 = "15 CANCUN ST",
SerialNo = 112156544,
XLatitude = 25.23456354,
YLongitude = -97.54435453,
Alarms = new List<Alarm>(new[]
{
new Alarm
{
AlarmId="Outage",
AlarmTime=DateTime.Now
},
new Alarm
{
AlarmId="Restore",
AlarmTime=DateTime.Now
}
})
};
XmlSerializer ser = new XmlSerializer(typeof(SqlOutput));
XDocument doc = new XDocument();
using (var writer = doc.CreateWriter())
{
ser.Serialize(writer, node);
}
Console.WriteLine(doc.ToString());
Console.ReadLine();
}
}
[XmlRoot("node")]
public class SqlOutput
{
[XmlElement("nodeid")]
public int NodeId { get; set; }
[XmlElement("account_no")]
public int AccountNumber { get; set; }
[XmlElement("address1")]
public string AddressLine1 { get; set; }
[XmlElement("serial_no")]
public int SerialNo { get; set; }
[XmlElement("x_lat")]
public double XLatitude { get; set; }
[XmlElement("y_lon")]
public double YLongitude { get; set; }
[XmlElement("alarm")]
public List<Alarm> Alarms { get; set; }
}
public class Alarm
{
public string AlarmId { get; set; }
public DateTime AlarmTime { get; set; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml.Serialization;
使用System.Xml.Linq;
命名空间SO.XmlSqlServer
{
班级计划
{
静态void Main(字符串[]参数)
{
var节点=新的SqlOutput
{
NodeId=28,
AccountNumber=0,
AddressLine1=“坎昆街15号”,
序列号=112156544,
XLatitude=25.23456354,
Y长度=-97.54435453,
报警=新列表(新[]
{
新警报
{
AlarmId=“停机”,
AlarmTime=DateTime.Now
},
新警报
{
AlarmId=“恢复”,
AlarmTime=DateTime.Now
}
})
};
XmlSerializer ser=新的XmlSerializer(typeof(SqlOutput));
XDocument doc=新XDocument();
使用(var writer=doc.CreateWriter())
{
序列化(编写器、节点);
}
Console.WriteLine(doc.ToString());
Console.ReadLine();
}
}
[XmlRoot(“节点”)]
公共类SqlOutput
{
[xmlement(“nodeid”)]
公共int节点ID{get;set;}
[XmlElement(“账号”)]
public int AccountNumber{get;set;}
[XmlElement(“地址1”)]
公共字符串AddressLine1{get;set;}
[XmlElement(“序列号”)]
public int SerialNo{get;set;}
[XmlElement(“x_lat”)]
公共双XLatitude{get;set;}
[XmlElement(“y_lon”)]
公共双Y长度{get;set;}
[XmlElement(“报警”)]
公共列表报警{get;set;}
}
公共类警报
{
公共字符串AlarmId{get;set;}
公共日期时间报警时间{get;set;}
}
}
我将输出显示到浏览器窗口,而不是控制台。另外,db中有两个节点28的条目(因此第二个xml中有两个节点),只需将Console.WriteLine()更改为HttpRequest.Write()或您用来编写代码的任何方法。我没有这样做,但这是一个好方法,我只是使用-for xml auto从sql返回xml文档。。并将其返回给C#XmlDocument对象,并通过Response.OutputStream将其写入XmlWriter。
protected void Page_Load(object sender, EventArgs e)
{
X();
writeXml();
}
private void X()
{
x = new XmlDocument();
using (SqlConnection con = new SqlConnection(cn))
{
using (SqlCommand cmd = new SqlCommand(sqlstring, con))
{
con.Open();
using (XmlReader reader = cmd.ExecuteXmlReader())
{
x.Load(reader);
}
}
}
}
private void writeXml()
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = true;
using (XmlWriter xr = XmlWriter.Create(Response.OutputStream, settings))
{
x.WriteContentTo(xr);
xr.Flush();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Xml.Linq;
namespace SO.XmlSqlServer
{
class Program
{
static void Main(string[] args)
{
var node = new SqlOutput
{
NodeId = 28,
AccountNumber = 0,
AddressLine1 = "15 CANCUN ST",
SerialNo = 112156544,
XLatitude = 25.23456354,
YLongitude = -97.54435453,
Alarms = new List<Alarm>(new[]
{
new Alarm
{
AlarmId="Outage",
AlarmTime=DateTime.Now
},
new Alarm
{
AlarmId="Restore",
AlarmTime=DateTime.Now
}
})
};
XmlSerializer ser = new XmlSerializer(typeof(SqlOutput));
XDocument doc = new XDocument();
using (var writer = doc.CreateWriter())
{
ser.Serialize(writer, node);
}
Console.WriteLine(doc.ToString());
Console.ReadLine();
}
}
[XmlRoot("node")]
public class SqlOutput
{
[XmlElement("nodeid")]
public int NodeId { get; set; }
[XmlElement("account_no")]
public int AccountNumber { get; set; }
[XmlElement("address1")]
public string AddressLine1 { get; set; }
[XmlElement("serial_no")]
public int SerialNo { get; set; }
[XmlElement("x_lat")]
public double XLatitude { get; set; }
[XmlElement("y_lon")]
public double YLongitude { get; set; }
[XmlElement("alarm")]
public List<Alarm> Alarms { get; set; }
}
public class Alarm
{
public string AlarmId { get; set; }
public DateTime AlarmTime { get; set; }
}
}