如何从XML文件中获取所有文本节点

时间:2022-06-01 18:15:36

I want to get all text nodes from an XML file.

我想从XML文件中获取所有文本节点。

How can I do this?

我怎样才能做到这一点?

Example Input:

示例输入:

<root>
<slide>
<Image>hi</Image>
<ImageContent>this</ImageContent>
<Thumbnail>is</Thumbnail>
<ThumbnailContent>A</ThumbnailContent>
</slide>
</root> 

Expected Output:

预期产出:

hi this is A

5 个解决方案

#1


2  

You can try this:

你可以试试这个:

        string input = @"
            <root>
                <slide>
                    <Image>hi</Image>
                    <ImageContent>this</ImageContent>
                    <Thumbnail>is</Thumbnail>
                    <ThumbnailContent>A</ThumbnailContent>
                </slide>
            </root>";

        XDocument doc = XDocument.Parse(input);
        //You can also load data from file by passing file path to Load method
        //XDocument doc = XDocument.Load("Data.xml");
        foreach(var slide in doc.Root.Elements("slide"))
        {
            var words = slide.Elements().Select(el => el.Value);
            string s = String.Join(" ", words.ToArray());
        }

#2


10  

The only solution (so far) to enumerate all text nodes in any xml, regardless of its structure:

唯一的解决方案(到目前为止)枚举任何xml中的所有文本节点,无论其结构如何:

string input = @"
    <root>
        <slide>
            <Image>hi</Image>
            <ImageContent>this</ImageContent>
            <Thumbnail>is</Thumbnail>
            <ThumbnailContent>A</ThumbnailContent>
        </slide>
    </root>";

foreach (XText text in (IEnumerable)XDocument.Parse(input).XPathEvaluate("//*/text()"))
{
    Console.WriteLine(text.Value);
}

EDIT: if you want to load xml from file then use XDocument.Load instead.

编辑:如果你想从文件加载xml然后使用XDocument.Load。

#3


3  

This code will print the inner text of all xml nodes which doesnt have a child:

此代码将打印没有子项的所有xml节点的内部文本:

    static void Main(string[] args)
    {
        XmlDocument x = new XmlDocument();
        x.Load("exp.xml");
        PrintNode(x.DocumentElement);
    }

    private static void PrintNode(XmlNode x)
    {
        if (!x.HasChildNodes)
            Console.Write(string.Format("{0} ", x.InnerText));

        for (int i = 0; i < x.ChildNodes.Count; i++)
        {
            PrintNode(x.ChildNodes[i]);
        }
    }

On your example XML it will result in the output you want :)

在您的示例XML上,它将产生您想要的输出:)

#4


2  

This will work

这会奏效

static void Main(string[] args)
        {            
            XDocument xmlSkuDescDoc = XDocument.Parse
                (@"<root>
                                <slide>
                                <Image>hi</Image>
                                <ImageContent>this</ImageContent>
                                <Thumbnail>is</Thumbnail>
                                <ThumbnailContent>A</ThumbnailContent>
                                </slide>
                                </root> "
                ); 
             var result = (from data in xmlSkuDescDoc.Descendants("slide")
             select data).Elements().Select(i => i.Value).Aggregate((a, b) => a + " " + b); 
            Console.ReadKey();
        }

N.B.~ use XDocument.Load(filename) if loading from file

N.B.~如果从文件加载,则使用XDocument.Load(filename)

e.g.

例如

string fileName = @"D:\MyXml.xml";
XDocument xmlSkuDescDoc = XDocument.Load(filename);

.... and the rest follows as shown above

....其余如下所示

#5


1  

It can be done using XDocument class (LINQ to XML). Assuming that you have exactly one slide element:

它可以使用XDocument类(LINQ to XML)完成。假设您只有一个幻灯片元素:

Using plain XDocument navigation:

使用普通的XDocument导航:

var doc = XDocument.Load("file path here");

if (doc.Root == null)
    throw new ArgumentException(); // No root node!
var slideElement = doc.Root.Element("slide");
if (slideElement == null)
    throw new ArgumentException(); // No slide node!

var values = string.Join(" ", slideElement.Elements().Select(element => element.Value));

Using XPath node selection:

使用XPath节点选择:

var doc = XDocument.Load("file path here");
var slideElements = doc.XPathSelectElements("root/slide/*");
var values = string.Join(" ", slideElements.Select(element => element.Value));

#1


2  

You can try this:

你可以试试这个:

        string input = @"
            <root>
                <slide>
                    <Image>hi</Image>
                    <ImageContent>this</ImageContent>
                    <Thumbnail>is</Thumbnail>
                    <ThumbnailContent>A</ThumbnailContent>
                </slide>
            </root>";

        XDocument doc = XDocument.Parse(input);
        //You can also load data from file by passing file path to Load method
        //XDocument doc = XDocument.Load("Data.xml");
        foreach(var slide in doc.Root.Elements("slide"))
        {
            var words = slide.Elements().Select(el => el.Value);
            string s = String.Join(" ", words.ToArray());
        }

#2


10  

The only solution (so far) to enumerate all text nodes in any xml, regardless of its structure:

唯一的解决方案(到目前为止)枚举任何xml中的所有文本节点,无论其结构如何:

string input = @"
    <root>
        <slide>
            <Image>hi</Image>
            <ImageContent>this</ImageContent>
            <Thumbnail>is</Thumbnail>
            <ThumbnailContent>A</ThumbnailContent>
        </slide>
    </root>";

foreach (XText text in (IEnumerable)XDocument.Parse(input).XPathEvaluate("//*/text()"))
{
    Console.WriteLine(text.Value);
}

EDIT: if you want to load xml from file then use XDocument.Load instead.

编辑:如果你想从文件加载xml然后使用XDocument.Load。

#3


3  

This code will print the inner text of all xml nodes which doesnt have a child:

此代码将打印没有子项的所有xml节点的内部文本:

    static void Main(string[] args)
    {
        XmlDocument x = new XmlDocument();
        x.Load("exp.xml");
        PrintNode(x.DocumentElement);
    }

    private static void PrintNode(XmlNode x)
    {
        if (!x.HasChildNodes)
            Console.Write(string.Format("{0} ", x.InnerText));

        for (int i = 0; i < x.ChildNodes.Count; i++)
        {
            PrintNode(x.ChildNodes[i]);
        }
    }

On your example XML it will result in the output you want :)

在您的示例XML上,它将产生您想要的输出:)

#4


2  

This will work

这会奏效

static void Main(string[] args)
        {            
            XDocument xmlSkuDescDoc = XDocument.Parse
                (@"<root>
                                <slide>
                                <Image>hi</Image>
                                <ImageContent>this</ImageContent>
                                <Thumbnail>is</Thumbnail>
                                <ThumbnailContent>A</ThumbnailContent>
                                </slide>
                                </root> "
                ); 
             var result = (from data in xmlSkuDescDoc.Descendants("slide")
             select data).Elements().Select(i => i.Value).Aggregate((a, b) => a + " " + b); 
            Console.ReadKey();
        }

N.B.~ use XDocument.Load(filename) if loading from file

N.B.~如果从文件加载,则使用XDocument.Load(filename)

e.g.

例如

string fileName = @"D:\MyXml.xml";
XDocument xmlSkuDescDoc = XDocument.Load(filename);

.... and the rest follows as shown above

....其余如下所示

#5


1  

It can be done using XDocument class (LINQ to XML). Assuming that you have exactly one slide element:

它可以使用XDocument类(LINQ to XML)完成。假设您只有一个幻灯片元素:

Using plain XDocument navigation:

使用普通的XDocument导航:

var doc = XDocument.Load("file path here");

if (doc.Root == null)
    throw new ArgumentException(); // No root node!
var slideElement = doc.Root.Element("slide");
if (slideElement == null)
    throw new ArgumentException(); // No slide node!

var values = string.Join(" ", slideElement.Elements().Select(element => element.Value));

Using XPath node selection:

使用XPath节点选择:

var doc = XDocument.Load("file path here");
var slideElements = doc.XPathSelectElements("root/slide/*");
var values = string.Join(" ", slideElements.Select(element => element.Value));