如何序列化为包含属性的 XML?

发布于 2024-08-26 00:16:42 字数 1514 浏览 8 评论 0原文

我有这样的代码:

...
request data = new request(); 
data.username = formNick; 
xml = data.Serialize();
...

[System.Serializable] 
public class request 
{ 
   public string username; 
   public string password;

   static XmlSerializer serializer = new XmlSerializer(typeof(request));
   public string Serialize() 
   { 
      StringBuilder builder = new StringBuilder(); 
      XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Encoding = Encoding.UTF8;
      serializer.Serialize(
         System.Xml.XmlWriter.Create(builder, settings ), 
         this); 

      return builder.ToString(); 
   } 
   public static request Deserialize(string serializedData) 
   { 
      return serializer.Deserialize(new StringReader(serializedData)) as request; 
   } 
}

我想向某些节点添加属性并创建一些子节点。另外如何像这样解析 xml:

<answer>
  <player id="2">
    <coordinate axis="x"></coordinate>
    <coordinate axis="y"></coordinate>
    <coordinate axis="z"></coordinate>
    <action name="nothing"></action>
  </player>
  <player id="3">
    <coordinate axis="x"></coordinate>
    <coordinate axis="y"></coordinate>
    <coordinate axis="z"></coordinate>
    <action name="boom">
      <1>1</1>
      <2>2</2>
    </action>
  </player>
</answer>

它不是一个 XML 文件,它是来自 HTTP 服务器的答案。

I have this code:

...
request data = new request(); 
data.username = formNick; 
xml = data.Serialize();
...

[System.Serializable] 
public class request 
{ 
   public string username; 
   public string password;

   static XmlSerializer serializer = new XmlSerializer(typeof(request));
   public string Serialize() 
   { 
      StringBuilder builder = new StringBuilder(); 
      XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Encoding = Encoding.UTF8;
      serializer.Serialize(
         System.Xml.XmlWriter.Create(builder, settings ), 
         this); 

      return builder.ToString(); 
   } 
   public static request Deserialize(string serializedData) 
   { 
      return serializer.Deserialize(new StringReader(serializedData)) as request; 
   } 
}

I want to add attributes to some nodes and create some sub-nodes. Also how to parse xml like this:

<answer>
  <player id="2">
    <coordinate axis="x"></coordinate>
    <coordinate axis="y"></coordinate>
    <coordinate axis="z"></coordinate>
    <action name="nothing"></action>
  </player>
  <player id="3">
    <coordinate axis="x"></coordinate>
    <coordinate axis="y"></coordinate>
    <coordinate axis="z"></coordinate>
    <action name="boom">
      <1>1</1>
      <2>2</2>
    </action>
  </player>
</answer>

It is not an XML file, it's an answer from HTTP server.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贱人配狗天长地久 2024-09-02 00:16:43

如果您有一个描述将从服务器接收到的 XML 的 XSD 文件,那就最好了。然后,您可以使用 XSD.EXE 程序生成具有适当 .NET 属性的 .NET 类。然后,您可以使用 XmlSerializer.Deserialize。

我将尝试手动为您创建这样一个类。这将是一个快速的尝试,并且可能是错误的(我必须回去工作!)


尝试一下,看看它是否有效。

using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;


[XmlRoot("answer")]
public class Answer
{
    [XmlElement]
    public List<Player> Players { get; set; }
}

public class Player
{
    [XmlAttribute("id")]
    public int ID { get; set; }

    [XmlElement]
    public List<Coordinate> Coordinates { get; set; }

    [XmlElement("action")]
    public PlayerAction Action { get; set; }
}

public class PlayerAction
{
    [XmlAttribute("name")]
    public string Name { get; set; }

    [XmlAnyElement]
    public XmlElement[] ActionContents { get; set; }
}

public enum Axis
{
    [XmlEnum("x")]
    X,
    [XmlEnum("y")]
    Y,
    [XmlEnum("z")]
    Z
}

public class Coordinate
{
    [XmlAttribute("axis")]
    public Axis Axis { get; set; }

    [XmlText]
    public double Value { get; set; }
}

It would be best if you had an XSD file describing the XML you will be receiving from the server. You could then use the XSD.EXE program to produce .NET classes with the appropriate .NET attributes on them. You could then just use XmlSerializer.Deserialize.

I'm going to try to create such a class for you by hand. This will be a quick attempt, and may be wrong (I have to get back to work!)


Try this and see if it works.

using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;


[XmlRoot("answer")]
public class Answer
{
    [XmlElement]
    public List<Player> Players { get; set; }
}

public class Player
{
    [XmlAttribute("id")]
    public int ID { get; set; }

    [XmlElement]
    public List<Coordinate> Coordinates { get; set; }

    [XmlElement("action")]
    public PlayerAction Action { get; set; }
}

public class PlayerAction
{
    [XmlAttribute("name")]
    public string Name { get; set; }

    [XmlAnyElement]
    public XmlElement[] ActionContents { get; set; }
}

public enum Axis
{
    [XmlEnum("x")]
    X,
    [XmlEnum("y")]
    Y,
    [XmlEnum("z")]
    Z
}

public class Coordinate
{
    [XmlAttribute("axis")]
    public Axis Axis { get; set; }

    [XmlText]
    public double Value { get; set; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文