我正在試驗用LinqToXSD創建XML數據綁定類,以及一個包含多個導入模式的XML模式。所有的模式都是located here.如何讓LinqToXSD正確輸出名稱空間前綴聲明?
要做到這一點,我用下面的根模式文檔:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:TmatsG="http://www.spiraltechinc.com/tmats/106-13/TmatsG" elementFormDefault="unqualified">
<xs:import namespace="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon" schemaLocation="TmatsCommonTypes.xsd"/>
<xs:import namespace="http://www.spiraltechinc.com/tmats/106-13/TmatsC" schemaLocation="TmatsCGroup.xsd"/>
<xs:import namespace="http://www.spiraltechinc.com/tmats/106-13/TmatsD" schemaLocation="TmatsDGroup.xsd"/>
<xs:import namespace="http://www.spiraltechinc.com/tmats/106-13/TmatsG" schemaLocation="TmatsGGroup.xsd"/>
<xs:import namespace="http://www.spiraltechinc.com/tmats/106-13/TmatsH" schemaLocation="TmatsHGroup.xsd"/>
<xs:import namespace="http://www.spiraltechinc.com/tmats/106-13/TmatsM" schemaLocation="TmatsMGroup.xsd"/>
<xs:import namespace="http://www.spiraltechinc.com/tmats/106-13/TmatsP" schemaLocation="TmatsPGroup.xsd"/>
<xs:import namespace="http://www.spiraltechinc.com/tmats/106-13/TmatsR" schemaLocation="TmatsRGroup.xsd"/>
<xs:import namespace="http://www.spiraltechinc.com/tmats/106-13/TmatsS" schemaLocation="TmatsSGroup.xsd"/>
<xs:import namespace="http://www.spiraltechinc.com/tmats/106-13/TmatsT" schemaLocation="TmatsTGroup.xsd"/>
<xs:import namespace="http://www.spiraltechinc.com/tmats/106-13/TmatsV" schemaLocation="TmatsVGroup.xsd"/>
<xs:element name="Tmats" type="TmatsG:Tmats">
<xs:annotation>
<xs:documentation>Tmats Root</xs:documentation>
</xs:annotation>
</xs:element>
</xs:schema>
我創建使用LINQ to XSD類。然後我寫了下面的測試:
[TestMethod()]
public void TmatsXmlExample4()
{
Tmats tmats = new Tmats
{
ProgramName = "My Program",
OriginationDate = DateTime.Now,
};
tmats.PointOfContact.Add(new PointOfContactType
{
Address = "12345 Anywhere Street",
Agency = "My Agency",
Name = "Robert Harvey",
Telephone = "111-222-3333"
});
Debug.Print(tmats.ToString());
}
我期望輸出看起來是這樣的:
<Tmats>
<TmatsG:ProgramName>My Program</TmatsG:ProgramName>
<TmatsG:OriginationDate>2012-05-09-07:00</TmatsG:OriginationDate>
<TmatsG:PointOfContact>
<TmatsCommon:Name>Robert Harvey</TmatsCommon:Name>
<TmatsCommon:Agency>My Agency</TmatsCommon:Agency>
<TmatsCommon:Address>12345 Anywhere Street</TmatsCommon:Address>
<TmatsCommon:Telephone>111-222-3333</TmatsCommon:Telephone>
</TmatsG:PointOfContact>
</Tmats>
相反,我得到的是這樣的:
<Tmats>
<ProgramName xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsG">My Program</ProgramName>
<OriginationDate xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsG">2012-05-09-07:00</OriginationDate>
<PointOfContact xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsG">
<Name xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon">Robert Harvey</Name>
<Agency xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon">My Agency</Agency>
<Address xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon">12345 Anywhere Street</Address>
<Telephone xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon">111-222-3333</Telephone>
</PointOfContact>
</Tmats>
有沒有一種辦法讓LinqToXSD產生預期的輸出?
謝謝! - 如果你確信模式不可能改變,他們可以註釋'Tmats.cs'文件中的所有元素來協助序列化程序,但是我個人只是將它轉換爲樣式表,然後離開它是。 –