我正在使用將新的 Person 物件附加到現有 Xml 檔案的 WebMethod。我想使用 XmlSerializer 來做到這一點,所以我不必手動進行序列化。到目前為止,我的物件按照我想要的方式進行了序列化。因此 Person 物件被傳遞給一個方法,該方法對其進行序列化并回傳一個帶有物件資訊的 XElement。問題是,當我將此 XElement 添加到 XDocument 的根時,會添加一個空的 xmlns 屬性。到目前為止,我嘗試添加空的 XmlSerializerNamespaces,我嘗試使用 Attribute-Remove 手動洗掉它,但我什至無法獲取該屬性,我得到的是我實際需要的 Id 屬性。有沒有辦法在沒有這種行為的情況下將 XElement 添加到 XDocument 中?任何光將不勝感激。謝謝。
這是我的 xml,最后一個人是使用 WebMethod 添加的:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="People-template.xslt" type="text/xsl"?>
<People xmlns="https://www.some-random_thing/SuperApp" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://www.some-random_thing/SuperApp People-schema.xsd">
<Person Id="1">
<Name>John Smith</Name>
<Phone>0299882211</Phone>
<Department>1</Department>
<Address>
<Street>1 Code Lane</Street>
<City>Javaville</City>
<State>CA</State>
<Zip>01003</Zip>
<Country>USA</Country>
</Address>
</Person>
<Person Id="2" xmlns="">
<Name>Sue White</Name>
<Phone>0388992255</Phone>
<Department>2</Department>
<Address>
<Street>16 Bit Way</Street>
<City>Byte Cove</City>
<State>QLD</State>
<Zip>1101</Zip>
<Country>Australia</Country>
</Address>
WebMethod 是這樣的:
[WebMethod]
public string InsertPerson(string name, string phone, int department, string street, string city, string state, int zip, string country)
{
Person newPerson = new Person()
{
Id = PersonXmlHelper.GetNextPersonId(),
Name = name,
Phone = phone,
Department = department,
Address = new Address()
{
Street = street,
City = city,
State = state,
Zip = zip,
Country = country
}
};
XElement personEl = PersonXmlHelper.ToXElement<Person>(newPerson);
doc.Root.Add(personEl);
doc.Save(filePath);
return "New person was successfully added";
}
這是我用來序列化 Person 的方法:
public static XElement ToXElement<T>(this object obj)
{
var serializer = new XmlSerializer(typeof(T));
XmlSerializerNamespaces xns = new XmlSerializerNamespaces();
xns.Add("", "");
XmlWriterSettings settings = new XmlWriterSettings()
{
Indent = true,
NamespaceHandling = NamespaceHandling.OmitDuplicates,
OmitXmlDeclaration = true
};
var sw = new StringWriter();
var xmlWriter = XmlWriter.Create(sw, settings);
serializer.Serialize(xmlWriter, obj, xns);
string xml = sw.ToString();
Debug.WriteLine(XElement.Parse(xml));
return XElement.Parse(xml);
}
Debug.Writeline 列印:
<Person Id="7">
<Name>Mary Jane</Name>
<Phone>0422888990</Phone>
<Department>2</Department>
<Address>
<Street>99 Integeration street</Street>
<City>Floating bay</City>
<State>SA</State>
<Zip>1111</Zip>
<Country>Australia</Country>
</Address>
</Person>
但是當我打開檔案時的最終結果是帶有 Id 的 Person 和與架構混亂的 xmlns="" 屬性。
uj5u.com熱心網友回復:
People及其后代元素在https://www.some-random_thing/SuperApp命名空間中(如xmlns屬性所示)。添加新元素時,必須將它們添加到相同的命名空間中。
您需要將適當的屬性應用于層次結構中的類。
[XmlRoot("Person", Namespace="https://www.some-random_thing/SuperApp")]
public class Person
{
[XmlElement("Name", Namespace="https://www.some-random_thing/SuperApp")]
public string Name { get; set; }
// ...
}
uj5u.com熱心網友回復:
經過許多其他嘗試,我通過向 XDocument.Save 方法添加 SaveOptions.OmitDuplicateNamespaces 引數來解決它。而且我還在檔案的根元素中添加了一個 Schema 宣告,因此它停止向每個新元素添加額外的屬性。我正在跳一段歡樂的舞蹈。有用!!!插入人方法:
[WebMethod]
public string InsertPerson(string name, string phone, int department, string street, string city, string state, int zip, string country)
{
Person newPerson = new Person()
{
Id = PersonXmlHelper.GetNextPersonId(),
Name = name,
Phone = phone,
Department = department,
Address = new Address()
{
Street = street,
City = city,
State = state,
Zip = zip,
Country = country
}
};
XElement personEl = PersonXmlHelper.ToXElement<Person>(newPerson);
Debug.WriteLine(personEl);
doc.Root.Add(personEl);
doc.Save(filePath, SaveOptions.OmitDuplicateNamespaces);
return "New person was successfully added";
}
將 Person 轉換為 XElement 的方法:
public static XElement ToXElement<T>(this object obj)
{
var serializer = new XmlSerializer(typeof(T), ns.NamespaceName);
XmlWriterSettings settings = new XmlWriterSettings()
{
OmitXmlDeclaration = true
};
var sw = new StringWriter();
var xmlWriter = XmlWriter.Create(sw, settings);
serializer.Serialize(xmlWriter, obj);
string xml = sw.ToString();
return XElement.Parse(xml);
}
根元素:
<People xmlns="https://www.some-random_thing/SuperApp" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:schemaLocation="https://www.some-random_thing/SuperApp People-schema.xsd">
最后是 Person 類:
public class Person
{
[XmlAttribute("Id")]
public int Id { get; set; }
public string Name { get; set; }
public string Phone { get; set; }
public int Department { get; set; }
public Address Address { get; set; }
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/337527.html
標籤:C# xml linq-to-xml asmx xml序列化器
