gpt4 book ai didi

c# - 从数据创建的 XML 结构中面临的问题

转载 作者:数据小太阳 更新时间:2023-10-29 02:15:21 24 4
gpt4 key购买 nike

我想通过数据创建 XML,并且我在 DataSet 中收到了来自数据库的数据。或 IList<Booking> .现在我正在使用 DataSet通过此代码创建 XML。

string result = String.Empty;

using (StringWriter sw = new StringWriter())
{
ds.WriteXml(sw);
result = sw.ToString();
}

我的 XML 就是这种形式。

<Booking> 
<ID>32</ID>
<BookingNumber>12120001</BLNumber>
<ReferenceNo>ABCED11212280007</ReferenceNo>
<Name>Customer Name1</Name>
<Address>Customer Address</Address>
</Booking>

<Booking>
<ID>33</ID>
<BookingNumber>12120002</BLNumber>
<ReferenceNo>ABCED11212280008</ReferenceNo>
<Name>Customer Name2</Name>
<Address>Customer Address2</Address>
</Booking>



<BookingDetail>
<ID>206</ID>
<BookingID>32</BookingID>
<OrderItem>Item1</OrderItem>
</BookingDetail>

<BookingDetail>
<ID>207</ID>
<BookingID>32</BookingID>
<OrderItem>Item2</OrderItem>
</BookingDetail>

<BookingDetail>
<ID>208</ID>
<BookingID>33</BookingID>
<OrderItem>Item1</OrderItem>
</BookingDetail>

<BookingDetail>
<ID>209</ID>
<BookingID>33</BookingID>
<OrderItem>Item2</OrderItem>
</BookingDetail>

<BookingDetail>
<ID>210</ID>
<BookingID>33</BookingID>
<OrderItem>Item3</OrderItem>
</BookingDetail>

但我想要这种形式的 XML。

<CompleteBooking>
<Booking>
<ID>32</ID>
<BookingNumber>12120001</BLNumber>
<ReferenceNo>ABCED11212280007</ReferenceNo>
<Name>Customer Name1</Name>
<Address>Customer Address</Address>
</Booking>

<BookingDetail>
<ID>206</ID>
<BookingID>32</BookingID>
<OrderItem>Item1</OrderItem>
</BookingDetail>

<BookingDetail>
<ID>207</ID>
<BookingID>32</BookingID>
<OrderItem>Item2</OrderItem>
</BookingDetail>

</CompleteBooking>

<CompleteBooking>
<Booking>
<ID>33</ID>
<BookingNumber>12120002</BLNumber>
<ReferenceNo>ABCED11212280008</ReferenceNo>
<Name>Customer Name2</Name>
<Address>Customer Address2</Address>
</Booking>

<BookingDetail>
<ID>208</ID>
<BookingID>33</BookingID>
<OrderItem>Item1</OrderItem>
</BookingDetail>

<BookingDetail>
<ID>209</ID>
<BookingID>33</BookingID>
<OrderItem>Item2</OrderItem>
</BookingDetail>

<BookingDetail>
<ID>210</ID>
<BookingID>33</BookingID>
<OrderItem>Item3</OrderItem>
</BookingDetail>

</CompleteBooking>

谁能帮我创建这种类型的 XML?

最佳答案

使用 System.Xml.Serialization,创建您的预订对象:

public class XMLEntities
{
[XmlRoot(ElementName = "CompleteBooking")]
public class CompleteBooking
{
[XmlElement(ElementName = "Booking")]
public Booking Bookings { get; set; }

[XmlElement(ElementName = "BookingDetail")]
public List<BookingDetail> BookingDetail { get; set; }
}

public class Booking
{
[XmlElement("ID")]
public int ID { get; set; }

[XmlElement("BookingNumber")]
public int BookingNumber { get; set; }

[XmlElement("ReferenceNumber")]
public string ReferenceNumber { get; set; }

[XmlElement("Name")]
public string Name { get; set; }

[XmlElement("Address")]
public string Address { get; set; }
}

public class BookingDetail
{
[XmlElement("ID")]
public int ID { get; set; }

[XmlElement("BookingID")]
public int BookingID { get; set; }

[XmlElement("OrderItem")]
public string OrderItem { get; set; }
}
}

现在,对于序列化器对象(用于实际将对象序列化为字符串):

 public class XMLEntitiesSerializer
{
public string Serialize(XMLEntities.CompleteBooking completeBooking)
{
var serializedXml = string.Empty;

var serializer = new XmlSerializer(typeof (XMLEntities.CompleteBooking));
var stringWriter = new System.IO.StringWriter();

try
{
serializer.Serialize(stringWriter, completeBooking);
serializedXml = stringWriter.ToString();
}
catch(Exception ex)
{
//Log the stuff
}
finally
{
stringWriter.Close();
}

return serializedXml;
}
}

现在,您只需创建正确定义的对象并在某种函数中进行序列化。例如,在控制台应用程序的主要方法中:

public static void Main(string[] args)
{
//Create new booking objects
var booking1 = new XMLEntities.Booking()
{
ID = 32,
BookingNumber = 1212001,
ReferenceNumber = "ABCED11212280007",
Name = "Customer Name1",
Address = "Customer Address"
};
var booking2 = new XMLEntities.Booking()
{
ID = 33,
BookingNumber = 12120002,
ReferenceNumber = "ABCED11212280008",
Name = "Customer Name2",
Address = "Customer Address2"
};

//Create the booking details objects

var booking1Detail1 = new XMLEntities.BookingDetail()
{
ID = 206,
BookingID = 32,
OrderItem = "Item1"
};
var booking1Detail2 = new XMLEntities.BookingDetail()
{
ID = 207,
BookingID = 32,
OrderItem = "Item2"
};

var booking2Detail1 = new XMLEntities.BookingDetail()
{
ID = 208,
BookingID = 33,
OrderItem = "Item1"
};
var booking2Detail2 = new XMLEntities.BookingDetail()
{
ID = 209,
BookingID = 32,
OrderItem = "Item2"
};
var booking2Detail3 = new XMLEntities.BookingDetail()
{
ID = 210,
BookingID = 32,
OrderItem = "Item3"
};

//Smash them together so we can serialize as one

var completeBooking1 = new XMLEntities.CompleteBooking()
{
Bookings = booking1,
BookingDetail = new List<XMLEntities.BookingDetail>()
{
booking1Detail1,
booking1Detail2
}
};
var completeBooking2 = new XMLEntities.CompleteBooking()
{
Bookings = booking2,
BookingDetail = new List<XMLEntities.BookingDetail>()
{
booking2Detail1,
booking2Detail2,
booking2Detail3
}
};

//Serialize the data for each of the booking objects

var serializedXML = new XMLEntitiesSerializer();
var xml = string.Empty;

var booking1XmlString = serializedXML.Serialize(completeBooking1);
var booking2XmlString = serializedXML.Serialize(completeBooking2);

Console.ReadLine();
}

显然,您可以在重构函数中使用它(这会让生活更轻松),但这会为您提供所需的一般输出。

关于c# - 从数据创建的 XML 结构中面临的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14889419/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com