c# - 获取除根元素之外的整个 XML

标签 c# xml linq

<Employees>
  <Product_Name>
    <hello1>product1</hello1>
    <hello2>product2</hello2>
    <hello3>product3</hello3>
    <hello4>product4</hello4>
  </Product_Name>
  <product_Price>
    <hello1>111</hello1>
    <hello2>222</hello2>
    <hello3>333</hello3>
    <hello4>444</hello4>
  </product_Price>
</Employees>

是否可以使用 C# 将以下 XML 转换为如下所示的 XML。我尝试使用删除功能,但没有用。我还尝试获取根节点的值。没用

  <Product_Name>
    <hello1>product1</hello1>
    <hello2>product2</hello2>
    <hello3>product3</hello3>
    <hello4>product4</hello4>
  </Product_Name>
  <product_Price>
    <hello1>111</hello1>
    <hello2>222</hello2>
    <hello3>333</hello3>
    <hello4>444</hello4>
  </product_Price>

最佳答案

如果您只想获取内部 xml,可以使用 XmlReader 的 ReadInnerXml。innerXML 作为字符串获取(跳过根节点)。

var xmlReader = XElement.Load("data.xml").CreateReader();
xmlReader.MoveToContent();
string innerXml = xmlReader.ReadInnerXml();

关于c# - 获取除根元素之外的整个 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13353799/

相关文章:

c# - 正确设置窗口图标并发生 XamlParseException

c# - LINQ 到 XML : A query body must end with a select clause or a group clause

c# - 更改 TabControl 未使用空间的颜色

xml - 如何允许 xml :lang attribute in XMLSchema?

c# - 如何找到给定时间跨度的所有数据

c# - "Argument data type ntext is invalid for argument 1 of upper function"尝试将第三个项目添加到过滤器时

c# - 使用 Encoding.GetBytes 或转换为字节的区别

java - 在代码中使用绑定(bind)类 - JIBX

sql - 在 Sql 中从 XML 中提取数据的 SUM

c# - 需要帮助将 C# foreach 循环转换为 lambda