C#XML - " XDocument"," XElement"," XNode" [英] C# XML - "XDocument","XElement", "XNode"

查看:80
本文介绍了C#XML - " XDocument"," XElement"," XNode"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用XDocumentXElement XNode ... Plz帮助...



XML结构:

how can this be done with "XDocument","XElement", "XNode"... Plz help...

XML STRUCTURE:

<Body>
  <getBooksResponse xmlns="http://tempuri.org/">
    <getBooksResult>
      <T>book2     </T>
      <A>author2   </A>
      <P>10</P>
    </getBooksResult>
  </getBooksResponse>
</Body>





1.删除带响应的节点,其中getBook可以是通用的,例如:getMapResponse等。



1. Remove Node with "Response" where "getBook" can be generic eg : "getMapResponse" etc.

&<getBooksResponse xmlns="http://tempuri.org/">





2.将节点XXXXResult替换为Book



2. Replace the Node "XXXXResult" with "Book"

推荐答案

这很容易使用 XDocument 的流利构造函数创建格式, XElement ,...



您可以这样做:



It''s easy to create the Format using the "fluent" constructors of XDocument, XElement, ...

You can do it like this:

public static XDocument CreateDocument()
        {
            return new XDocument(
                 new XElement("Body",
                     new XElement("getBooksResponse", new XAttribute("ns", "http://tempuri.org/"),
                         new XElement("getBooksResult",
                             new XElement("T", "book2"),
                             new XElement("A", "author2"),
                             new XElement("P", "10")
                         )
                     )
                  )
              );
        }





(如果您将属性命名为xmlns,则XML无效)



对不起,我不明白第1,2点的意思......



但我建议你使用表示数据的类层次结构,并使用 DataContractSerializer 生成XML。


这篇关于C#XML - &quot; XDocument&quot;,&quot; XElement&quot;,&quot; XNode&quot;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆