2
<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>
4

3 に答える 3

3

内部xmlをフェッチするだけの場合は、XmlReaderのを使用できReadInnerXmlます。innerXMLは文字列としてフェッチされます(ルートノードをスキップします)。

var xmlReader = XElement.Load("data.xml").CreateReader();
xmlReader.MoveToContent();
string innerXml = xmlReader.ReadInnerXml();
于 2012-11-13T01:57:55.507 に答える
2

Linq to XMLにアクセスできる場合は、そうする必要がありますXDocument
警告:あなたのxmlは不正な形式であると思います-あなたは次のようなものを持っているべきです:

<?xml version="1.0" encoding="utf-8"?>

最初のxmlタグをxmlに追加します。

string xml = @"<?xml version="1.0" encoding="utf-8"?>
               <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>";

XDocument xDoc = XDocument.Parse(xml);

// get the elements
var rootElements = xDoc.Root.Elements();

または、ファイルからxmlをロードすることもできます。

XDocument xDoc = XDocument.Load("xmlFile.xml");
于 2012-11-13T00:39:27.807 に答える
1

整形式になるようにXmlNodeListとして保存できます。これを行う方法の実際の例を次に示します。

XmlDocument xml= new XmlDocument();
        xml.LoadXml(@"<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>");
        var nodeList = xml.SelectNodes("Employees");
        foreach (XmlNode node in nodeList)
        {
           Console.WriteLine(node.InnerXml); //this will give you your desired result
        }
于 2012-11-13T00:27:27.287 に答える