简体   繁体   English

在C#中序列化XML

[英]Serializing XML in C#

I usually search the web high and low for my answers but this time I am drawing a blank. 我通常会在网上上下搜索我的答案,但这一次我是空白。 I'm using VS2005 to write code to POST xml to an API. 我正在使用VS2005将代码写入POST xml到API。 I have classes setup in C# that I serialize into an XML document. 我在C#中安装了类,并将其序列化为XML文档。 The classes are below: 这些类如下:

[Serializable]
    [XmlRoot(Namespace = "", IsNullable = false)]
    public class Request
    {
        public RequestIdentify Identify;

        public string Method;

        public string Params;

    }

    [Serializable]
    public class RequestIdentify
    {
        public string StoreId;

        public string Password;
    }

When I serialize this I get: 当我将此序列化时,我得到:

<?xml version="1.0" encoding="UTF-8"?>
<Request xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <Identify>
      <StoreId>00</StoreId>
      <Password>removed for security</Password>
   </Identify>
   <Method>ProductExport</Method>
   <Params />
</Request>

But the API returns a "No XML sent" error. 但是API返回“未发送XML”错误。

If I send the xml directly in a string as: 如果我直接以字符串形式发送xml:

string xml = @"<Request><Identify><StoreId>00</StoreId><Password>Removed for security</Password></Identify><Method>ProductExport</Method><Params /></Request>";

effectively sending this xml (without the schema info in the "Request" tag): 有效地发送此xml(在“ Request”标签中没有架构信息):

<Request>
   <Identify>
      <StoreId>00</StoreId>
      <Password>Removed for security</Password>
   </Identify>
   <Method>ProductExport</Method>
   <Params />
</Request>

It seems to recognise the XML no problem. 似乎认识到XML没问题。

So my question I guess is how can I change my current classes to Serialize into XML and get the XML as in the second case? 所以我想问的问题是,如何才能将当前的类更改为Serialize并转换为XML并像第二种情况一样获取XML? I assume I need another "parent" class to wrap around the existing ones and use InnerXml property on this "parent" or something similar but I don't know how to do this. 我假设我需要另一个“父”类来包装现有类,并在该“父”或类似对象上使用InnerXml属性,但我不知道该怎么做。

Apologies for the question, I've only been using C# for 3 months and I'm a trainee developer who is having to teach himself on the job! 抱歉,我仅使用C#3个月,而且我是一名受训开发人员,必须在工作中自学!

Oh and PS I don't know why but VS2005 really does not want to let me set these classes up with private variables and then use getters and setters on public equivalents so I have them written how they are for now. 哦和PS我不知道为什么,但是VS2005确实不想让我使用私有变量来设置这些类,然后在公共等效项上使用getter和setter,所以现在让他们编写它们的状态。

Thanks in advance :-) 提前致谢 :-)

UPDATE: 更新:

As with most things it's very hard to find answers if you're not sure what you need to ask or how to word it but: 与大多数事情一样,如果不确定要问什么或如何措词,则很难找到答案,但是:

Once I knew what to look for I found the answers I needed: 知道要查找的内容后,我便找到了所需的答案:

Removing the XML declaration: 删除XML声明:

XmlWriterSettings writerSettings = new XmlWriterSettings();
writerSettings.OmitXmlDeclaration = true;
StringWriter stringWriter = new StringWriter();
using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, writerSettings))
{
    serializer.Serialize(xmlWriter, request);
}
string xmlText = stringWriter.ToString();

Removing/Setting the namespace (Thanks to above replies that helped find this one!): 删除/设置名称空间(感谢上述答复帮助找到了这个名称!):

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");

Thanks for your help everyone who answered or pointed me in the right direction! 感谢您的帮助,每个回答或指出我正确方向的人! And yes I did find articles to read once I knew what I was asking :-) It's the first time I have come unstuck in 3 months of teaching myself so I think I'm doing pretty well... 是的,当我知道自己在问什么时,我的确找到了可供阅读的文章:-)这是我三个月来第一次自学成才,所以我觉得我做得不错。

From Rydal's blog: 来自Rydal的博客:

The XmlDocument object by default assigns a namespace to the XML string and also includes the declaration as the first line of the XML document. 默认情况下,XmlDocument对象为XML字符串分配一个名称空间,并且还包括该声明作为XML文档的第一行。 I definitely do not need or use those, therefore, I need to remove them. 我绝对不需要或不使用它们,因此,我需要将其删除。 Here is how you go about doing just that. 这就是您要做的事情。

Removing declaration and namespaces from XML serialization 从XML序列化中删除声明和名称空间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM