XML 직렬화에 대해 microsoft tutorial을 따르려고하고 있지만 몇 가지 문제가 있습니다!C#의 XML 직렬화
:public class OrderedItem
{
[XmlElement(Namespace = "http://www.cpandl.com")]
public string ItemName;
[XmlElement(Namespace = "http://www.cpandl.com")]
public string Description;
[XmlElement(Namespace = "http://www.cohowinery.com")]
public decimal UnitPrice;
[XmlElement(Namespace = "http://www.cpandl.com")]
public int Quantity;
[XmlElement(Namespace = "http://www.cohowinery.com")]
public decimal LineTotal;
// A custom method used to calculate price per item.
public void Calculate()
{
LineTotal = UnitPrice * Quantity;
}
}
이 기능은 'OrderedItem'클래스로 XML 읽기 :이 클래스는 XML을 결합하는 것입니다
<?xml version="1.0" encoding="utf-8"?>
<Books xmlns:books="http://www.cpandl.com" xmlns:money="http://www.cohowinery.com">
<money:Book>
<books:TITLE>A Book Title</books:TITLE>
<money:PRICE books:currency="US Dollar">
<money:price>9.95</money:price>
</money:PRICE>
</money:Book>
</Books>
:
이
입력으로 사용되는 XML 파일입니다Console.WriteLine("Reading with Stream");
// Create an instance of the XmlSerializer.
var serializer = new XmlSerializer(typeof(OrderedItem));
// Reading the XML document requires a FileStream.
Stream reader = new FileStream(filename, FileMode.Open);
// Declare an object variable of the type to be deserialized.
// Call the Deserialize method to restore the object's state.
var i = (OrderedItem)serializer.Deserialize(reader);
Console.SetOut(new StreamWriter("a_output.xml"));
serializer.Serialize(Console.Out, i);
이것은 읽은 후 다시 쓰는 XML입니다.
<?xml version="1.0" encoding="utf-8"?>
<OrderedItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ItemName xmlns="http://www.cpandl.com">Widget</ItemName>
<Description xmlns="http://www.cpandl.com">Regular Widget</Description>
<UnitPrice xmlns="http://www.cohowinery.com">2.3</UnitPrice>
<Quantity xmlns="http://www.cpandl.com">10</Quantity>
<LineTotal xmlns="http://www.cohowinery.com">23</LineTotal>
</OrderedItem>
네임 스페이스가 확장되었습니다. uri 대신 네임 스페이스 레이블이있는 동일한 XML을 얻으려면 출력을 어떻게 작성해야합니까?