如何序列化为包含属性的 XML?
我有这样的代码:
...
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您有一个描述将从服务器接收到的 XML 的 XSD 文件,那就最好了。然后,您可以使用 XSD.EXE 程序生成具有适当 .NET 属性的 .NET 类。然后,您可以使用 XmlSerializer.Deserialize。
我将尝试手动为您创建这样一个类。这将是一个快速的尝试,并且可能是错误的(我必须回去工作!)
尝试一下,看看它是否有效。
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.