如何忽略自动生成的属性并在其位置添加新属性?
How to neglect automatically generated attributes and add new attributes in its place?
我正在创建一个 XML 使用序列化的节点,使用 C#
<node xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<childNode1><![CDATA[some file name]]></childNode1>
<childNode2><![CDATA[some file name]]></childNode2>
</node >
如何避免“节点”的属性 "xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
并在其位置添加其他属性?
因此,结果节点应如下所示:
<node name="xyz" group="abc">
我使用的代码是:
class Program
{
public static void Main(string[] args)
{
AddressDetails details = new AddressDetails();
Serialize(details);
}
static public void Serialize(AddressDetails details)
{
XmlSerializer serializer = new XmlSerializer(typeof(AddressDetails));
TextWriter writer = new StreamWriter(@"C:\Xml.xml");
serializer.Serialize(writer, details);
}
}
public class AddressDetails
{
[XmlElement("Street")]
public string StreetName { get; set; }
[XmlElement("CityName")]
public string City { get; set; }
public Address address;
public AddressDetails()
{
StreetName = "XYZ";
City = "Pune";
}
public System.Xml.XmlCDataSection MyStringCDATA
{
get
{
return new System.Xml.XmlDocument().CreateCDataSection("load and run");
}
set
{ }
}
}
public class Address
{
[XmlAttribute("HouseNo")]
public int HouseNo { get; set; }
[XmlAttribute("floor")]
public int floor { get; set; }
public Address()
{
HouseNo = 204;
floor = 2;
}
}
创建这个xml的目的是,我想多次创建相同的节点模板,但每个节点名称都不同,我想动态给出节点名称。
例如。在上面的代码中,节点名称是通过 class 名称设置的 "AddressDetails" .....现在我希望节点名称为 "AddressDetails_John"..."AddressDetails_Harry".. 是否有任何规定以这种方式使其动态化? –
谢谢...
要禁止写入标准 XML 命名空间,将空的 XmlSerializerNamespaces
传递给 XmlSerializer.Serialize()
:
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", ""); // Disable the xmlns:xsi and xmlns:xsd lines.
serializer.Serialize(xmlWriter, obj, ns);
添加一个属性:当一个属性被标记为[XmlAttribute]
属性时,它会被序列化为XML作为一个属性,例如:
[XmlAttribute("name")]
public string Name { get; set; }
因此,在您的示例中,如果您希望 AddressDetails.Street
和 AddressDetails.CityName
成为属性,您可以这样做:
public class AddressDetails
{
[XmlAttribute("Street")]
public string StreetName { get; set; }
[XmlAttribute("CityName")]
public string City { get; set; }
public Address address;
public AddressDetails()
{
StreetName = "XYZ";
City = "Pune";
}
public System.Xml.XmlCDataSection MyStringCDATA
{
get
{
return new System.Xml.XmlDocument().CreateCDataSection("load and run");
}
set
{ }
}
}
并且,要在序列化时省略标准名称空间,您可以添加以下静态方法:
public static void Serialize<T>(T obj, string filename, bool omitStandardNamespaces = false) // Change the default to true if you prefer.
{
using (var textWriter = new StreamWriter(filename))
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true; // For cosmetic purposes.
settings.IndentChars = " "; // For cosmetic purposes.
using (var xmlWriter = XmlWriter.Create(textWriter, settings))
{
if (omitStandardNamespaces)
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", ""); // Disable the xmlns:xsi and xmlns:xsd lines.
serializer.Serialize(xmlWriter, obj, ns);
}
else
{
serializer.Serialize(xmlWriter, obj);
}
}
}
}
我正在创建一个 XML 使用序列化的节点,使用 C#
<node xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<childNode1><![CDATA[some file name]]></childNode1>
<childNode2><![CDATA[some file name]]></childNode2>
</node >
如何避免“节点”的属性 "xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
并在其位置添加其他属性?
因此,结果节点应如下所示:
<node name="xyz" group="abc">
我使用的代码是:
class Program
{
public static void Main(string[] args)
{
AddressDetails details = new AddressDetails();
Serialize(details);
}
static public void Serialize(AddressDetails details)
{
XmlSerializer serializer = new XmlSerializer(typeof(AddressDetails));
TextWriter writer = new StreamWriter(@"C:\Xml.xml");
serializer.Serialize(writer, details);
}
}
public class AddressDetails
{
[XmlElement("Street")]
public string StreetName { get; set; }
[XmlElement("CityName")]
public string City { get; set; }
public Address address;
public AddressDetails()
{
StreetName = "XYZ";
City = "Pune";
}
public System.Xml.XmlCDataSection MyStringCDATA
{
get
{
return new System.Xml.XmlDocument().CreateCDataSection("load and run");
}
set
{ }
}
}
public class Address
{
[XmlAttribute("HouseNo")]
public int HouseNo { get; set; }
[XmlAttribute("floor")]
public int floor { get; set; }
public Address()
{
HouseNo = 204;
floor = 2;
}
}
创建这个xml的目的是,我想多次创建相同的节点模板,但每个节点名称都不同,我想动态给出节点名称。 例如。在上面的代码中,节点名称是通过 class 名称设置的 "AddressDetails" .....现在我希望节点名称为 "AddressDetails_John"..."AddressDetails_Harry".. 是否有任何规定以这种方式使其动态化? –
谢谢...
要禁止写入标准 XML 命名空间,将空的
XmlSerializerNamespaces
传递给XmlSerializer.Serialize()
:XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); // Disable the xmlns:xsi and xmlns:xsd lines. serializer.Serialize(xmlWriter, obj, ns);
添加一个属性:当一个属性被标记为
[XmlAttribute]
属性时,它会被序列化为XML作为一个属性,例如:[XmlAttribute("name")] public string Name { get; set; }
因此,在您的示例中,如果您希望 AddressDetails.Street
和 AddressDetails.CityName
成为属性,您可以这样做:
public class AddressDetails
{
[XmlAttribute("Street")]
public string StreetName { get; set; }
[XmlAttribute("CityName")]
public string City { get; set; }
public Address address;
public AddressDetails()
{
StreetName = "XYZ";
City = "Pune";
}
public System.Xml.XmlCDataSection MyStringCDATA
{
get
{
return new System.Xml.XmlDocument().CreateCDataSection("load and run");
}
set
{ }
}
}
并且,要在序列化时省略标准名称空间,您可以添加以下静态方法:
public static void Serialize<T>(T obj, string filename, bool omitStandardNamespaces = false) // Change the default to true if you prefer.
{
using (var textWriter = new StreamWriter(filename))
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true; // For cosmetic purposes.
settings.IndentChars = " "; // For cosmetic purposes.
using (var xmlWriter = XmlWriter.Create(textWriter, settings))
{
if (omitStandardNamespaces)
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", ""); // Disable the xmlns:xsi and xmlns:xsd lines.
serializer.Serialize(xmlWriter, obj, ns);
}
else
{
serializer.Serialize(xmlWriter, obj);
}
}
}
}